0

我正在使用 .NET 4.5 中的 Entity Framework 5.0。我正在尝试构建一个按类型表 (TPT) 继承层次结构,其中我有一个指向子类之一的主键的外键。不幸的是,实体框架给了我以下编译错误:

错误 3024:从第 163 行开始映射片段时出现问题:必须为关系 FK_Items_Derived1 中 End Derived1 的所有关键属性 (Id) 指定映射。

我创建了一个测试数据库和 EF 模型来展示该模型。我的数据库模型如下所示:

在此处输入图像描述

要创建实体框架模型我:

  • 将这些表添加到实体框架设计器;
  • 选择Base作为基础类型Derived1Derived2;
  • Derived1删除了and 和andBase之间的外键关系(因为现在有继承关系);Derived2Base
  • 我从and中删除了Id属性(因为它们从 继承了属性)。Derived1Derived2IdBase

结果是以下模型:

在此处输入图像描述

现在当我编译它时,Entity Framework 会提示我前面提到的编译错误:

错误 3024:从第 163 行开始映射片段时出现问题:必须为关系 FK_Items_Derived1 中 End Derived1 的所有关键属性 (Id) 指定映射。

该错误似乎指向模型的 XML 映射中的以下行:

<AssociationSetMapping Name="FK_Items_Derived1" 
    TypeName="TestModel.FK_Items_Derived1" StoreEntitySet="Items">
    <EndProperty Name="Items">
        <ScalarProperty Name="Id" ColumnName="Id" />
    </EndProperty>
</AssociationSetMapping>

我显然不希望Items将 映射到Base类,因为只有Derived1项目,而不是Derived2. 我不明白为什么实体框架设计者不能处理这个相当常见的用例。

所以问题当然是如何解决这个问题?

为了完整起见,这里是完整的实体框架映射文件:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="TestModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
        <EntityContainer Name="TestModelStoreContainer">
          <EntitySet Name="Base" EntityType="TestModel.Store.Base" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Derived1" EntityType="TestModel.Store.Derived1" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Derived2" EntityType="TestModel.Store.Derived2" store:Type="Tables" Schema="dbo" />
          <EntitySet Name="Items" EntityType="TestModel.Store.Items" store:Type="Tables" Schema="dbo" />
          <AssociationSet Name="FK_Derived1_Base" Association="TestModel.Store.FK_Derived1_Base">
            <End Role="Base" EntitySet="Base" />
            <End Role="Derived1" EntitySet="Derived1" />
          </AssociationSet>
          <AssociationSet Name="FK_Derived2_Base" Association="TestModel.Store.FK_Derived2_Base">
            <End Role="Base" EntitySet="Base" />
            <End Role="Derived2" EntitySet="Derived2" />
          </AssociationSet>
          <AssociationSet Name="FK_Items_Derived1" Association="TestModel.Store.FK_Items_Derived1">
            <End Role="Derived1" EntitySet="Derived1" />
            <End Role="Items" EntitySet="Items" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Base">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
        </EntityType>
        <EntityType Name="Derived1">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Length" Type="int" Nullable="false" />
        </EntityType>
        <EntityType Name="Derived2">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Size" Type="int" Nullable="false" />
        </EntityType>
        <EntityType Name="Items">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Derived1Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Description" Type="nvarchar" Nullable="false" MaxLength="50" />
        </EntityType>
        <Association Name="FK_Derived1_Base">
          <End Role="Base" Type="TestModel.Store.Base" Multiplicity="1" />
          <End Role="Derived1" Type="TestModel.Store.Derived1" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived1">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Derived2_Base">
          <End Role="Base" Type="TestModel.Store.Base" Multiplicity="1" />
          <End Role="Derived2" Type="TestModel.Store.Derived2" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived2">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Items_Derived1">
          <End Role="Derived1" Type="TestModel.Store.Derived1" Multiplicity="1" />
          <End Role="Items" Type="TestModel.Store.Items" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Derived1">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Items">
              <PropertyRef Name="Derived1Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="TestModel" Alias="Self" p1:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:p1="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityContainer Name="Entities" p1:LazyLoadingEnabled="true">
          <EntitySet Name="Base" EntityType="TestModel.Base" />
          <EntitySet Name="Items" EntityType="TestModel.Items" />
          <AssociationSet Name="FK_Items_Derived1" Association="TestModel.FK_Items_Derived1">
            <End Role="Derived1" EntitySet="Base" />
            <End Role="Items" EntitySet="Items" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Base">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Guid" Nullable="false" />
          <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
        </EntityType>
        <EntityType Name="Derived1" BaseType="TestModel.Base">
          <Property Name="Length" Type="Int32" Nullable="false" />
          <NavigationProperty Name="Items" Relationship="TestModel.FK_Items_Derived1" FromRole="Derived1" ToRole="Items" />
        </EntityType>
        <EntityType Name="Derived2" BaseType="TestModel.Base">
          <Property Name="Size" Type="Int32" Nullable="false" />
        </EntityType>
        <EntityType Name="Items">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Guid" Nullable="false" />
          <Property Name="Description" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
          <NavigationProperty Name="Derived1" Relationship="TestModel.FK_Items_Derived1" FromRole="Items" ToRole="Derived1" />
        </EntityType>
        <Association Name="FK_Items_Derived1">
          <End Role="Derived1" Type="TestModel.Derived1" Multiplicity="1" />
          <End Role="Items" Type="TestModel.Items" Multiplicity="*" />
        </Association>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="TestModelStoreContainer" CdmEntityContainer="Entities">
          <EntitySetMapping Name="Base">
            <EntityTypeMapping TypeName="IsTypeOf(TestModel.Base)">
              <MappingFragment StoreEntitySet="Base">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Name" ColumnName="Name" />
              </MappingFragment>
            </EntityTypeMapping>
            <EntityTypeMapping TypeName="IsTypeOf(TestModel.Derived2)">
              <MappingFragment StoreEntitySet="Derived2">
                <ScalarProperty Name="Size" ColumnName="Size" />
                <ScalarProperty Name="Id" ColumnName="Id" />
              </MappingFragment>
            </EntityTypeMapping>
            <EntityTypeMapping TypeName="IsTypeOf(TestModel.Derived1)">
              <MappingFragment StoreEntitySet="Derived1">
                <ScalarProperty Name="Length" ColumnName="Length" />
                <ScalarProperty Name="Id" ColumnName="Id" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Items">
            <EntityTypeMapping TypeName="TestModel.Items">
              <MappingFragment StoreEntitySet="Items">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Description" ColumnName="Description" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <AssociationSetMapping Name="FK_Items_Derived1" TypeName="TestModel.FK_Items_Derived1" StoreEntitySet="Items">
            <EndProperty Name="Items">
              <ScalarProperty Name="Id" ColumnName="Id" />
            </EndProperty>
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="False" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="False" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>

对于想要在本地重现此内容的任何人,这里是 DDL 脚本:

CREATE TABLE [dbo].[Base](
    [Id] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Base] PRIMARY KEY CLUSTERED ([Id] ASC)
 WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Derived1](
    [Id] [uniqueidentifier] NOT NULL,
    [Length] [int] NOT NULL,
 CONSTRAINT [PK_Derived1] PRIMARY KEY CLUSTERED ([Id] ASC) 
 WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Derived2](
    [Id] [uniqueidentifier] NOT NULL,
    [Size] [int] NOT NULL,
 CONSTRAINT [PK_Derived2] PRIMARY KEY CLUSTERED ([Id] ASC)
 WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Items](
    [Id] [uniqueidentifier] NOT NULL,
    [Derived1Id] [uniqueidentifier] NOT NULL,
    [Description] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ([Id] ASC)
 WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
 ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Derived1]  WITH CHECK ADD  CONSTRAINT [FK_Derived1_Base] FOREIGN KEY([Id])
REFERENCES [dbo].[Base] ([Id])
GO

ALTER TABLE [dbo].[Derived1] CHECK CONSTRAINT [FK_Derived1_Base]
GO

ALTER TABLE [dbo].[Derived2]  WITH CHECK ADD  CONSTRAINT [FK_Derived2_Base] FOREIGN KEY([Id])
REFERENCES [dbo].[Base] ([Id])
GO

ALTER TABLE [dbo].[Derived2] CHECK CONSTRAINT [FK_Derived2_Base]
GO

ALTER TABLE [dbo].[Items]  WITH CHECK ADD  CONSTRAINT [FK_Items_Derived1] FOREIGN KEY([Derived1Id])
REFERENCES [dbo].[Derived1] ([Id])
GO

ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Derived1]
GO
4

3 回答 3

1

这是使用 TPT 映射的模型配置。

    <?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
      <Schema Namespace="tempdatabase1Model1.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2008" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
        <EntityType Name="Base">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Name" Type="nvarchar" MaxLength="50" Nullable="false" />
        </EntityType>
        <EntityType Name="Derived1">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Length" Type="int" Nullable="false" />
        </EntityType>
        <EntityType Name="Derived2">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Size" Type="int" Nullable="false" />
        </EntityType>
        <EntityType Name="Items">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Derived1Id" Type="uniqueidentifier" Nullable="false" />
          <Property Name="Description" Type="nvarchar" MaxLength="50" Nullable="false" />
        </EntityType>
        <Association Name="FK_Derived1_Base">
          <End Role="Base" Type="Self.Base" Multiplicity="1" />
          <End Role="Derived1" Type="Self.Derived1" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived1">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Derived2_Base">
          <End Role="Base" Type="Self.Base" Multiplicity="1" />
          <End Role="Derived2" Type="Self.Derived2" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived2">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Items_Derived1">
          <End Role="Derived1" Type="Self.Derived1" Multiplicity="1" />
          <End Role="Items" Type="Self.Items" Multiplicity="*" />
          <ReferentialConstraint>
            <Principal Role="Derived1">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Items">
              <PropertyRef Name="Derived1Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <EntityContainer Name="tempdatabase1Model1StoreContainer">
          <EntitySet Name="Base" EntityType="Self.Base" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="Derived1" EntityType="Self.Derived1" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="Derived2" EntityType="Self.Derived2" Schema="dbo" store:Type="Tables" />
          <EntitySet Name="Items" EntityType="Self.Items" Schema="dbo" store:Type="Tables" />
          <AssociationSet Name="FK_Derived1_Base" Association="Self.FK_Derived1_Base">
            <End Role="Base" EntitySet="Base" />
            <End Role="Derived1" EntitySet="Derived1" />
          </AssociationSet>
          <AssociationSet Name="FK_Derived2_Base" Association="Self.FK_Derived2_Base">
            <End Role="Base" EntitySet="Base" />
            <End Role="Derived2" EntitySet="Derived2" />
          </AssociationSet>
          <AssociationSet Name="FK_Items_Derived1" Association="Self.FK_Items_Derived1">
            <End Role="Derived1" EntitySet="Derived1" />
            <End Role="Items" EntitySet="Items" />
          </AssociationSet>
        </EntityContainer>
      </Schema>
    </edmx:StorageModels>
    <!-- CSDL content -->
    <edmx:ConceptualModels>
      <Schema Namespace="tempdatabase1Model1" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
        <EntityType Name="Base">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Guid" Nullable="false" />
          <Property Name="Name" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" />
        </EntityType>
        <EntityType Name="Derived1" BaseType="tempdatabase1Model1.Base">
          <Property Name="Length" Type="Int32" Nullable="false" />
          <NavigationProperty Name="Items" Relationship="Self.FK_Items_Derived1" FromRole="Derived1" ToRole="Items" />
        </EntityType>
        <EntityType Name="Derived2" BaseType="tempdatabase1Model1.Base">
          <Property Name="Size" Type="Int32" Nullable="false" />
        </EntityType>
        <EntityType Name="Item">
          <Key>
            <PropertyRef Name="Id" />
          </Key>
          <Property Name="Id" Type="Guid" Nullable="false" />
          <Property Name="Description" Type="String" MaxLength="50" FixedLength="false" Unicode="true" Nullable="false" />
          <NavigationProperty Name="Derived1" Relationship="Self.FK_Items_Derived1" FromRole="Items" ToRole="Derived1" />
        </EntityType>
        <Association Name="FK_Derived1_Base">
          <End Role="Base" Type="Self.Base" Multiplicity="1" />
          <End Role="Derived1" Type="Self.Derived1" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived1">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Derived2_Base">
          <End Role="Base" Type="Self.Base" Multiplicity="1" />
          <End Role="Derived2" Type="Self.Derived2" Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Base">
              <PropertyRef Name="Id" />
            </Principal>
            <Dependent Role="Derived2">
              <PropertyRef Name="Id" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
        <Association Name="FK_Items_Derived1">
          <End Role="Derived1" Type="Self.Derived1" Multiplicity="1" />
          <End Role="Items" Type="Self.Item" Multiplicity="*" />
        </Association>
        <EntityContainer Name="tempdatabase1Entities1" annotation:LazyLoadingEnabled="true">
          <EntitySet Name="Bases" EntityType="Self.Base" />
          <EntitySet Name="Items" EntityType="Self.Item" />
          <AssociationSet Name="FK_Derived1_Base" Association="Self.FK_Derived1_Base">
            <End Role="Base" EntitySet="Bases" />
            <End Role="Derived1" EntitySet="Bases" />
          </AssociationSet>
          <AssociationSet Name="FK_Derived2_Base" Association="Self.FK_Derived2_Base">
            <End Role="Base" EntitySet="Bases" />
            <End Role="Derived2" EntitySet="Bases" />
          </AssociationSet>
          <AssociationSet Name="FK_Items_Derived1" Association="Self.FK_Items_Derived1">
            <End Role="Derived1" EntitySet="Bases" />
            <End Role="Items" EntitySet="Items" />
          </AssociationSet>
        </EntityContainer>
      </Schema>
    </edmx:ConceptualModels>
    <!-- C-S mapping content -->
    <edmx:Mappings>
      <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
        <EntityContainerMapping StorageEntityContainer="tempdatabase1Model1StoreContainer" CdmEntityContainer="tempdatabase1Entities1">
          <EntitySetMapping Name="Bases">
            <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Base)">
              <MappingFragment StoreEntitySet="Base">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Name" ColumnName="Name" />
              </MappingFragment>
            </EntityTypeMapping>
            <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Derived1)">
              <MappingFragment StoreEntitySet="Derived1">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Length" ColumnName="Length" />
              </MappingFragment>
            </EntityTypeMapping>
            <EntityTypeMapping TypeName="IsTypeOf(tempdatabase1Model1.Derived2)">
              <MappingFragment StoreEntitySet="Derived2">
                <ScalarProperty Name="Size" ColumnName="Size" />
                <ScalarProperty Name="Id" ColumnName="Id" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <EntitySetMapping Name="Items">
            <EntityTypeMapping TypeName="tempdatabase1Model1.Item">
              <MappingFragment StoreEntitySet="Items">
                <ScalarProperty Name="Id" ColumnName="Id" />
                <ScalarProperty Name="Description" ColumnName="Description" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          <AssociationSetMapping Name="FK_Items_Derived1" TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items">
            <EndProperty Name="Derived1">
              <ScalarProperty Name="Id" ColumnName="Derived1Id" />
            </EndProperty>
            <EndProperty Name="Items">
              <ScalarProperty Name="Id" ColumnName="Id" />
            </EndProperty>
          </AssociationSetMapping>
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>
  </edmx:Runtime>
  <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
  <Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx">
    <Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </Connection>
    <Options>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="ValidateOnBuild" Value="true" />
        <DesignerProperty Name="EnablePluralization" Value="true" />
        <DesignerProperty Name="IncludeForeignKeysInModel" Value="false" />
        <DesignerProperty Name="UseLegacyProvider" Value="true" />
        <DesignerProperty Name="CodeGenerationStrategy" Value="None" />
      </DesignerInfoPropertySet>
    </Options>
    <!-- Diagram content (shape and connector positions) -->
    <Diagrams></Diagrams>
  </Designer>
</edmx:Edmx>
于 2013-10-29T18:58:48.190 回答
1

一个实体框架代码优先解决方案的价值。

在此处查看完整的博客文章:http:
//jnye.co/Posts/18/table-per-type-tpt-database-using-entityframework-code-first

模型

public abstract class BaseTable
{
    public int Id { get; set; }
    public string Name { get; set; }
}

[Table("DerivedWithRelation")]
public class DerivedWithRelation : BaseTable
{
    public int Amount { get; set; }
    public string About { get; set; }
    public int RelatedId { get; set; }

    public virtual ICollection<Relation> Relations { get; set; }
}

[Table("DerivedWithoutRelation")]
public class DerivedWithoutRelation : BaseTable
{
    public int Quantity { get; set; }
    public string Description { get; set; }
}

public class Relation
{
    public int Id { get; set; }
    public string RelationshipType { get; set; }

    public virtual DerivedWithRelation DerivedWithRelation { get; set; }
}

上下文

public class MyContext : DbContext
{
    public MyContext()
        : base("DefaultConnection")
    {            
    }
    
    public IDbSet<BaseTable> BaseTables { get; set; }
    public IDbSet<DerivedWithRelation> DerivedWithRelations { get; set; }
    public IDbSet<DerivedWithoutRelation> DerivedWithoutRelations { get; set; }
}

首先使用实体​​框架代码和 TPT 创建的模式
(来源:jnye.co

希望有帮助。

于 2013-10-30T09:26:43.697 回答
0

@Jdev4ls 指出我的答案。通过从模型中删除主键(来自Derived1),实体框架会丢失一些信息来进行关联。我不清楚如何防止这种情况发生,我相信设计师应该让这更简单,因为这是 IMO 一个非常常见的用例

但是,您可以更新 XML 或在设计器中执行此操作。使用上面的示例,搜索以下 XML:

<AssociationSetMapping Name="FK_Items_Derived1"
    TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items">
    <EndProperty Name="Items">
        <ScalarProperty Name="Id" ColumnName="Id" />
    </EndProperty>
</AssociationSetMapping>

并添加一个带有指向的EndPropertyfor像这样:DerivedScalarPropertyDerived1Id

<AssociationSetMapping Name="FK_Items_Derived1" 
    TypeName="tempdatabase1Model1.FK_Items_Derived1" StoreEntitySet="Items">
    <EndProperty Name="Derived1">
        <ScalarProperty Name="Id" ColumnName="Derived1Id" />
    </EndProperty>
    <EndProperty Name="Items">
        <ScalarProperty Name="Id" ColumnName="Id" />
    </EndProperty>
</AssociationSetMapping>

如果您想使用设计器(就像我一样)执行此操作,请执行以下操作:

  • Items右键单击和之间的关系Derived1
  • 在上下文菜单中选择“表映射”。
  • 将显示此关联的映射详细信息,您会立即看到其中一个列丢失。您可以Derived1Id uniqueidentifier从下拉列表中选择,然后您就完成了。
  • 请注意,如果Derive1实体具有自己的外键属性,您可能还需要修复它们,但实体框架似乎交换了列。填写缺失的列后,您可能需要交换两列。
于 2013-10-29T19:03:23.140 回答