2009-12-09 4 views
1

J'essaye de faire un modèle Table par Hiérarchie dans Entity Framework (VS 2008 sp1, 3.5).Entity Framework TPH avec héritage abstrait multiple

La plupart de mes modèles ont été très simples, un type abstrait avec plusieurs sous-types qui en héritent.

Cependant, j'ai eu du mal avec ce dernier défi. J'ai des ÉTUDIANTS que j'aimerais hériter de PERSONNES (abstrait) qui devraient hériter de PARTIES (abstrait).

Chaque fois que je fais cela, je reçois un "Erreur 2078: L'EntityType 'Model.PERSONS' est abstrait et peut être mappé uniquement en utilisant IsTypeOf." Je suppose que le problème est PARTIES est déjà défini comme IsTypeOf dans l'ensemble d'entités.

Est-ce encore possible? Je peux contourner cela en faisant des PERSONNES abstract = false et en assignant un faux mapping conditionnel. Mais cela semble être une solution de contournement stupide.

+0

Suivi question ici: http://stackoverflow.com/questions/2045924/multiple-inheritance-with-entity-framework-with-tph – itchi

Répondre

2

Modifiez le modèle à l'aide de l'éditeur XML: recherchez le mappage et ajoutez IsTypeOf à la balise EntityTypeMapping correspondante.
Voici un exemple qui ressemble à votre cas:
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

Intéressant .. Donc il semble que je doive faire toutes les entités TypeOf dans l'espace de mappage "CS" si elles sont abstraites. Toute entité qui n'est pas abstraite n'a pas besoin de cette modification. Votre exemple a aidé, merci pour la réponse et désolé pour le retard (vacances et je voulais m'assurer que je l'ai bien) – itchi