Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle entity in VS entity framework doesnt update the primary key in code

I have an entity called Tree, which has a primary key called Id (with StoreGeneratedPattern = Identity) that when using the following code, correctly inserts into the database.

   using(TestContainer tss = new TestContainer())
   {
      Tree tree = new Tree()
      {
         Name = "TestTree"
      };

      tss.Trees.AddObject(tree);
      tss.SaveChanges();
   }

I have a backing sequence + trigger to handle the auto incremented primary key Id. I have verified that this actually inserts into the database correctly.

Calling tss.Refresh(System.Data.Objects.RefreshMode.StoreWins, tree); does not update the object (the 'Id' field is still 0). Any ideas?

        <EntityType Name="Tree">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="Name" Nullable="false" />
          <NavigationProperty Name="HeuristicCulls" Relationship="TOPSSSimpleSelect.TreeHeuristicCull" FromRole="Tree" ToRole="HeuristicCull" />
          <Property Type="Int32" Name="Type" Nullable="false" />
          <NavigationProperty Name="PR_T" Relationship="TOPSSSimpleSelect.PR_TTree" FromRole="Tree" ToRole="PR_T" />
          <NavigationProperty Name="TreeItems" Relationship="TOPSSSimpleSelect.TreeTreeItem" FromRole="Tree" ToRole="TreeItem" />
          <Property Type="Byte" Name="IsRoot" Nullable="false" />
          <Property Type="Byte" Name="IsProductRoot" Nullable="false" />
          <NavigationProperty Name="TreeProducts" Relationship="TOPSSSimpleSelect.T_TP" FromRole="Tree" ToRole="TreeProducts" />
        </EntityType>
        <EntityType Name="TreeItem">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <NavigationProperty Name="Questions" Relationship="TOPSSSimpleSelect.TI_Q" FromRole="TreeItem" ToRole="Question" />
          <Property Type="String" Name="Name" Nullable="false" />
          <NavigationProperty Name="SubmitRules" Relationship="TOPSSSimpleSelect.SubmitRuleTreeItem" FromRole="TreeItem" ToRole="SubmitRule" />
          <NavigationProperty Name="PR_TI" Relationship="TOPSSSimpleSelect.PR_TITreeItem" FromRole="TreeItem" ToRole="PR_TI" />
          <NavigationProperty Name="Tree" Relationship="TOPSSSimpleSelect.TreeTreeItem" FromRole="TreeItem" ToRole="Tree" />
          <Property Type="Int32" Name="TreeId" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="Byte" Name="IsRoot" Nullable="false" />
        </EntityType>
like image 865
Jason Avatar asked Sep 08 '11 16:09

Jason


People also ask

How to create model from an existing DB in Entity Framework?

I am using the shiny new ODT 19.3 via VS 2019. When ever I try to open the Entity Framework wizard to create a model from a existing DB the wizard closes without any feedback. 1. Add new ADO.NET Entity Data Model 2. EF Designer from database 3. Choose Your Data Connection and click Next 4. BOOM

How do I change the identity column in an EF model?

Double-click the .edmx file, then locate your table in the database diagram, highlight the identity column in the diagram, and in the properties window, change the StoreGeneratedPattern value to Identity. That should make it persist even when you update your EF model. Show activity on this post.

Does Entity Framework Wizard work with Visual Studio 2019?

Entity Framework Wizard does NOT work (doesn't appear!) However, since that discussion has been deemed to be 'answered', it appears I cannot add to it. I have Visual Studio 2019 (16.7.2) I created a C# console application (.NET Framework) - n.b. NOT .NET Core OracleManagedDataAccess (v18.3.0)


1 Answers

Open up the .edmx file with the XML editor and look for the section that begins with the following line:

<!-- SSDL content -->

Below should be an EntityType tag and in it is a definition of the database table. Make sure that the property for your ID column has StoreGeneratedPattern="Identity" in it.

Below this SSDL section is a CSDL section that looks similar, but defines the C# object that represents this entity. The visual designer only seems to fill in the StoreGeneratedPatternin this section, but not the SSDL section.

EDIT WITH SAMPLE

Here's a sample EDMX file for an Employee entity, with nothing but an ID, FirstName, and LastName property. The ID is the field you want auto generated by the database. Note that there are two different places where StoreGeneratedPattern is needed.

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="Store" Alias="Self" Provider="Oracle.DataAccess.Client" ProviderManifestToken="10g"
              xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
              xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
        <EntityContainer Name="StoreContainer">
          <EntitySet Name="EMPLOYEE" EntityType="Store.EMPLOYEE" store:Type="Tables" Schema="TESTSPACE" />
        </EntityContainer>
        <EntityType Name="EMPLOYEE">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <!-- The below property requires StoreGeneratedPattern="Identity" -->
          <Property Name="ID" Type="number" StoreGeneratedPattern="Identity" Nullable="false" Precision="10" />
          <Property Name="FIRST_NAME" Type="varchar2" MaxLength="255" />
          <Property Name="LAST_NAME" Type="varchar2" MaxLength="255" />
        </EntityType>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm"
              xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration"
              xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model" Alias="Self"
              xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
        <EntityContainer Name="ModelContainer" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="Employees1" EntityType="Model.Employee" />
        </EntityContainer>
        <EntityType Name="Employee">
          <Key>
            <PropertyRef Name="ID" />
          </Key>
          <!-- The below property requires StoreGeneratedPattern="Identity" -->
          <Property Type="Int32" Name="ID" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
          <Property Type="String" Name="FirstName" MaxLength="255" FixedLength="false" Unicode="false" />
          <Property Type="String" Name="LastName" MaxLength="255" FixedLength="false" Unicode="false" />
        </EntityType>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="StoreContainer" CdmEntityContainer="ModelContainer">
          <EntitySetMapping Name="Employees1">
            <EntityTypeMapping TypeName="Model.Employee">
              <MappingFragment StoreEntitySet="EMPLOYEE">
                <ScalarProperty Name="LastName" ColumnName="LAST_NAME" />
                <ScalarProperty Name="FirstName" ColumnName="FIRST_NAME" />
                <ScalarProperty Name="ID" ColumnName="ID" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
    <edmx:Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="True" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="True" />
      </DesignerInfoPropertySet>
    </edmx:Options>
    <!-- Diagram content (shape and connector positions) -->
    <edmx:Diagrams>
      <Diagram Name="Model">
        <EntityTypeShape EntityType="Model.Employee" Width="1.5" PointX="0.75" PointY="0.75"
                         Height="1.4279589843749996" />
      </Diagram>
    </edmx:Diagrams>
  </edmx:Designer>
</edmx:Edmx>
like image 59
Simon T Avatar answered Sep 30 '22 19:09

Simon T