在VS实体框架的Oracle实体犯规更新code中的主键 [英] Oracle entity in VS entity framework doesnt update the primary key in code

查看:187
本文介绍了在VS实体框架的Oracle实体犯规更新code中的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫做树的实体,它有一个名为ID的主键(与StoreGeneratedPattern =标识),使用下面的code时,正确地插入到数据库中。

 使用(TestContainer TSS =新TestContainer())
   {
      树树=新树()
      {
         NAME =TestTree
      };      tss.Trees.AddObject(树);
      tss.SaveChanges();
   }

我有一个后盾序列+触发器来处理自动递增的主键标识。我已经验证,这实际上插入到数据库中正确。

调用 tss.Refresh(System.Data.Objects.RefreshMode.StoreWins,树); 不更新对象(ID字段仍然为0)。任何想法?

 <的EntityType名称=树>
          <关键>
            < PropertyRef名称=ID/>
          < / Key与GT;
          <物业类型=的Int32NAME =ID可空=false的注释:StoreGeneratedPattern =身份/>
          <物业类型=字符串NAME =姓名可空=假/>
          < NavigationProperty NAME =HeuristicCulls关系=TOPSSSimpleSelect.TreeHeuristicCullFromRole =树ToRole =HeuristicCull/>
          <物业类型=的Int32NAME =类型可空=假/>
          < NavigationProperty NAME =PR_T关系=TOPSSSimpleSelect.PR_TTreeFromRole =树ToRole =PR_T/>
          < NavigationProperty NAME =TreeItems关系=TOPSSSimpleSelect.TreeTreeItemFromRole =树ToRole =TreeItem/>
          <物业类型=字节NAME =IsRoot可空=假/>
          <物业类型=字节NAME =IsProductRoot可空=假/>
          < NavigationProperty NAME =TreeProducts关系=TOPSSSimpleSelect.T_TPFromRole =树ToRole =TreeProducts/>
        < /&的EntityType GT;
        <的EntityType名称=TreeItem>
          <关键>
            < PropertyRef名称=ID/>
          < / Key与GT;
          <物业类型=的Int32NAME =ID可空=false的注释:StoreGeneratedPattern =身份/>
          < NavigationProperty NAME =问题的关系=TOPSSSimpleSelect.TI_QFromRole =TreeItemToRole =问题/>
          <物业类型=字符串NAME =姓名可空=假/>
          < NavigationProperty NAME =SubmitRules关系=TOPSSSimpleSelect.SubmitRuleTreeItemFromRole =TreeItemToRole =SubmitRule/>
          < NavigationProperty NAME =PR_TI关系=TOPSSSimpleSelect.PR_TITreeItemFromRole =TreeItemToRole =PR_TI/>
          < NavigationProperty NAME =树的关系=TOPSSSimpleSelect.TreeTreeItemFromRole =TreeItemToRole =树/>
          <物业类型=的Int32NAME =TreeId可空=false的注释:StoreGeneratedPattern =身份/>
          <物业类型=字节NAME =IsRoot可空=假/>
        < /&的EntityType GT;


解决方案

在XML编辑器中打开.edmx文件并查找与以下行开头的部分:

 <! -  SSDL内容 - >

下面应该是一个的EntityType代码,并在它的数据库中的表的定义。请确保您的ID列的属性具有的 StoreGeneratedPattern =身份的在里面。

下面这个SSDL部分是CSDL部分看起来相似,但定义了C#对象重新presents这个实体。视觉设计师似乎只在StoreGeneratedPatternin填补了这一部分,但不是SSDL部分。

WITH SAMPLE EDIT

下面是一个Employee实体样本EDMX文件中,只是一个ID,名字,和姓氏属性。该ID是要通过数据库自动生成现场。请注意,有在需要StoreGeneratedPattern两个不同的地方。

 <?XML版本=1.0编码=UTF-8&GT?;
< EDMX:EDMX版本=2.0的xmlns:EDMX =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
  <! - EF运行时的内容 - >
  < EDMX:运行>
    <! - SSDL内容 - >
    < EDMX:StorageModels>
      <模式命名空间=商店别名=自我提供程序=Oracle.DataAccess.ClientProviderManifestToken =10克
              的xmlns:店=htt​​p://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator
              的xmlns =htt​​p://schemas.microsoft.com/ado/2009/02/edm/ssdl>
        < EntityContainer相关NAME =StoreContainer>
          < EntitySet的名称=雇员的EntityType =Store.EMPLOYEE店:TYPE =表模式=TESTSPACE/>
        < / EntityContainer相关>
        <的EntityType名称=雇员>
          <关键>
            < PropertyRef名称=ID/>
          < / Key与GT;
          <! - 下面的属性要求StoreGeneratedPattern =身份 - >
          <属性名称=ID类型=数字StoreGeneratedPattern =身份可空=假precision =10/>
          <属性名=FIRST_NAMETYPE =VARCHAR2MAXLENGTH =255/>
          <属性名=LAST_NAMETYPE =VARCHAR2MAXLENGTH =255/>
        < /&的EntityType GT;
      < /模式>
    < / EDMX:StorageModels>
    <! - CSDL内容 - >
    < EDMX:ConceptualModels>
      <模式的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/edm
              的xmlns:CG =htt​​p://schemas.microsoft.com/ado/2006/04/$c$cgeneration
              的xmlns:店=htt​​p://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator命名空间=模型别名=自我
              的xmlns:注释=htt​​p://schemas.microsoft.com/ado/2009/02/edm/annotation>
        < EntityContainer相关NAME =ModelContainer注释:LazyLoadingEnabled =真正的>
          < EntitySet的名称=雇员1的EntityType =Model.Employee/>
        < / EntityContainer相关>
        <的EntityType名称=雇员>
          <关键>
            < PropertyRef名称=ID/>
          < / Key与GT;
          <! - 下面的属性要求StoreGeneratedPattern =身份 - >
          <物业类型=的Int32NAME =ID可空=false的注释:StoreGeneratedPattern =身份/>
          <物业类型=字符串名称=名字MAXLENGTH =255定长=false的统一code =FALSE/>
          <物业类型=字符串名称=姓氏MAXLENGTH =255定长=false的统一code =FALSE/>
        < /&的EntityType GT;
      < /模式>
    < / EDMX:ConceptualModels>
    <! - C-S映射内容 - >
    < EDMX:映射>
      <映射空间=C-S的xmlns =htt​​p://schemas.microsoft.com/ado/2008/09/mapping/cs>
        < EntityContainerMapping StorageEntityContainer =StoreContainerCdmEntityContainer =ModelContainer>
          < EntitySetMapping NAME =雇员1>
            < EntityTypeMapping类型名=Model.Employee>
              < MappingFragment StoreEntitySet =雇员>
                < ScalarProperty名称=姓氏的ColumnName =LAST_NAME/>
                < ScalarProperty名称=名字的ColumnName =FIRST_NAME/>
                < ScalarProperty名称=ID的ColumnName =ID/>
              < / MappingFragment>
            < / EntityTypeMapping>
          < / EntitySetMapping>
        < / EntityContainerMapping>
      < /映射>
    < / EDMX:映射>
  < / EDMX:运行>
  <! - EF设计内容(不要手动编辑BELOW HERE) - GT;
  < EDMX:设计师的xmlns =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
    < EDMX:连接>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty NAME =MetadataArtifactProcessingVALUE =EmbedInOutputAssembly/>
      < / DesignerInfoPropertySet>
    < / EDMX:连接>
    < EDMX:选项>
      < D​​esignerInfoPropertySet>
        < D​​esignerProperty NAME =ValidateOnBuildVALUE =真/>
        < D​​esignerProperty NAME =EnablePluralizationVALUE =真/>
        < D​​esignerProperty NAME =IncludeForeignKeysInModelVALUE =真/>
      < / DesignerInfoPropertySet>
    < / EDMX:选项>
    <! - 图的内容(形状和连接器的位置) - GT;
    < EDMX:图>
      <图名称=模型>
        &所述; EntityTypeShape的EntityType =Model.Employee宽度=1.5PointX =0.75尖=0.75
                         HEIGHT =1.4279589843749996/>
      < /图>
    < / EDMX:图>
  < / EDMX:设计师>
< / EDMX:EDMX>

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>

解决方案

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>

这篇关于在VS实体框架的Oracle实体犯规更新code中的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆