2009-12-09 473 views
1

我試圖在實體框架(VS 2008 SP1,3.5)中執行Table Per Hierarchy模型。具有多個抽象繼承的實體框架TPH

我的大多數模型都非常簡單,它是一個抽象類型,它具有多個繼承自它的子類型。

但是,我一直在爲這個最新的挑戰而苦苦掙扎。我有學生想繼承人(抽象),應該從派生(抽象)繼承。

每次我這樣做,我得到一個「錯誤2078:EntityType'Model.PERSONS'是抽象的,只能使用IsTypeOf映射。」我想問題是PARTIES已經在實體集中定義爲IsTypeOf。

那麼這甚至可能嗎?我可以通過使PERSONS abstract = false並分配一個假條件映射來解決它。但這似乎是一個愚蠢的解決方法。

+0

跟進的問題在這裏: http://stackoverflow.com/questions/2045924/multiple-inheritance-with-entity-framework-with-tph – itchi 2010-01-12 00:25:26

回答

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

所以我似乎必須在「CS」映射空間中創建所有實體TypeOf(如果它們是抽象的)。任何非抽象的實體都不需要編輯。你的例子幫助了,謝謝你的回答,併爲延遲感到抱歉(節假日,我想確保我做對了) – itchi 2009-12-18 23:43:32