c# - VS Entity Framework 中的Oracle实体不更新代码中的主键

标签 c# visual-studio visual-studio-2010 oracle entity-framework

我有一个名为 Tree 的实体,它有一个名为 Id 的主键(StoreGeneratedPattern = Identity),当使用以下代码时,它会正确地插入到数据库中。

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

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

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

调用 tss.Refresh(System.Data.Objects.RefreshMode.StoreWins, tree); 不会更新对象(“Id”字段仍为 0)。有什么想法吗?

        <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>

最佳答案

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

<!-- SSDL content -->

下面应该是一个 EntityType 标签,里面是数据库表的定义。确保 ID 列的属性中有 StoreGeneratedPattern="Identity"

在这个 SSDL 部分下面是一个 CSDL 部分,它看起来很相似,但定义了代表这个实体的 C# 对象。视觉设计者似乎只在这部分填写了StoreGeneratedPattern,而没有填写SSDL部分。

用样本编辑

这是一个 Employee 实体的示例 EDMX 文件,除了 ID、FirstName 和 LastName 属性外什么都没有。 ID 是您希望数据库自动生成的字段。请注意,有两个不同的地方需要 StoreGeneratedPattern。

<?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>

关于c# - VS Entity Framework 中的Oracle实体不更新代码中的主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7351260/

相关文章:

visual-studio - 在 Visual Studio 2019 中使用 VSCode 键盘快捷键

c++ - 访问 QTimer 的经过秒数?

visual-studio-2010 - Visual Studio 2010 调试器 - 结构调试信息仍保留旧成员名称和类型,不更新

visual-studio-2010 - 你能在 vs 2010 中根据 xml 验证 xsd 吗?

c# - 如何以编程方式将 Excel Power 查询中的连接字符串更改为 SQL Server?

c# - 如何在不添加对调用项目的引用的情况下通过类库正确解析 DLL 引用

c# - 使用 Aforge.Net 将灰度图像转换为黑白图像

c# - Exchange EWS 文件夹同步仅获取编程更改

c++ - 为什么我没有收到枚举比较不匹配的警告?

visual-studio - 自动添加 Visual Studio "External Tools..."菜单选项的 PowerShell 脚本?