具有多个抽象继承的实体框架TPH

时间:2009-12-09 22:34:36

标签: entity-framework table-per-hierarchy

我正在尝试在Entity Framework中执行Table Per Hierarchy模型(VS 2008 sp1,3.5)。

我的大多数模型都非常简单,一种抽象类型,包含多个从中继承的子类型。

然而,我一直在努力应对这一最新挑战。我有学生,我想继承PERSONS(摘要),这应该是PARTIES(摘要)的继承。

每次执行此操作时,我都会收到“错误2078:EntityType'Mode.PERSONS'是抽象的,只能使用IsTypeOf进行映射。”我想问题是PARTIES已经在实体集中被定义为IsTypeOf。

这甚至可能吗?我可以通过使PERSONS abstract = false并分配伪条件映射来解决它。但这似乎是一个愚蠢的解决方法。

1 个答案:

答案 0 :(得分:2)

使用XML Editor编辑模型:找到映射并将IsTypeOf添加到相应的EntityTypeMapping标记。
以下是一个类似于您案例的示例:
SQL Server DDL:

CREATE TABLE [dbo].[Student] (
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [PartyInfo] [varchar](max) NOT NULL,
    [PersonInfo] [varchar](max) NOT NULL,
    [StudInfo] [varchar](max) NOT NULL,
    CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED (  [Id] ASC  )
  )

EDMX:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" 
xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
  <Schema Namespace="test_1Model.Store" Alias="Self" 
Provider="System.Data.SqlClient" ProviderManifestToken="2005" 
xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    <EntityContainer Name="test_1ModelStoreContainer">
      <EntitySet Name="Student" EntityType="test_1Model.Store.Student" 
store:Type="Tables" Schema="dbo" />
    </EntityContainer>
    <EntityType Name="Student">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="int" Nullable="false" 
StoreGeneratedPattern="Identity" />
      <Property Name="PartyInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="PersonInfo" Type="varchar(max)" Nullable="false" />
      <Property Name="StudInfo" Type="varchar(max)" Nullable="false" />
    </EntityType>
  </Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
  <Schema Namespace="test_1Model" Alias="Self" 
xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
    <EntityContainer Name="test_Entities">
      <EntitySet Name="PartySet" EntityType="test_1Model.Party" />
    </EntityContainer>
    <EntityType Name="Party" Abstract="true">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Name="Id" Type="Int32" Nullable="false" />
      <Property Name="PartyInfo" Type="String" Nullable="false" 
MaxLength="Max" Unicode="false" FixedLength="false" />
    </EntityType>
    <EntityType Name="Person" BaseType="test_1Model.Party" Abstract="true" >
      <Property Name="PersonInfo" Type="String" Nullable="false" />
    </EntityType>
    <EntityType Name="Student" BaseType="test_1Model.Person" >
      <Property Name="StudInfo" Type="String" Nullable="false" />
    </EntityType>
  </Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
  <Mapping Space="C-S" 
xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
    <EntityContainerMapping 
StorageEntityContainer="test_1ModelStoreContainer" 
CdmEntityContainer="test_Entities">
      <EntitySetMapping Name="PartySet">
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Party)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="IsTypeOf(test_1Model.Person)">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
          </MappingFragment>
        </EntityTypeMapping>
        <EntityTypeMapping TypeName="test_1Model.Student">
          <MappingFragment StoreEntitySet="Student">
            <ScalarProperty Name="PartyInfo" ColumnName="PartyInfo" />
            <ScalarProperty Name="PersonInfo" ColumnName="PersonInfo" />
            <ScalarProperty Name="Id" ColumnName="Id" />
            <ScalarProperty Name="StudInfo" ColumnName="StudInfo" />
          </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/2007/06/edmx">
    <edmx:Connection>
      <DesignerInfoPropertySet>
        <DesignerProperty Name="MetadataArtifactProcessing" 
Value="EmbedInOutputAssembly" />
      </DesignerInfoPropertySet>
    </edmx:Connection>
  <edmx:Options>
    <DesignerInfoPropertySet>
      <DesignerProperty Name="ValidateOnBuild" Value="true" />
    </DesignerInfoPropertySet>
  </edmx:Options>
  <!-- Diagram content (shape and connector positions) -->
  <edmx:Diagrams>
    <Diagram Name="SqlServer_Model" />
  </edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>