Yesterday, I published an article on CodeProject about Table Per Hierarchy (TPH) in Entity Framework. I got a very good question about “How would the EF handle this with a true association table?” – which means removing the two dates from the Person table and creating an association table instead that will hold only one date field. This post will try to answer the
question.
The Database Diagram
In order to answer the question, I first created the appropriate database.
The figure below shows its diagram:
We have a person table which holds the first and last name of the person and also its type.
This table has an associated table with a one to one mapping which holds the person date (which can be enrolment date for student and hire date for professor).
Creating the Model
When you create the EDM, you will first get the following model:
In order to create the inheritance tree (by imposing TPH) and using Entity Splitting to connect the PersonDateInfo into the created entities, follow these steps.
Step 1
Remove the PersonDateInfo entity by deleting it. After that, add the entities of Student and Professor to the model which inherit from Person:
Step 2
Remove the PersonType from Person, map Person also to the PersonDateInfo table and make Person abstract:
Step 3
Create a new property on Student – EnrollementDate with DateTime type and a new property on Professor – HireDate which is also a DateTime. Map the entities to Person table and also to PersonDateInfo.
Create conditional mapping on PersonType for both entities (PersonType 1 is professor and PersonType 2 is student). Also create a conditional mapping on the DateType for both entities (DateType 1 for HireDate and dateType 2 for EnrollementDate).
After all the changes, the mapping should be:
The model itself will look like:
Step 4
When you compile the model, you’ll get error 3024: Must specify mapping for all key properties. This error rises because the mapping for the PersonDateInfo table doesn’t include mapping for the PersonID for the inheriting entities. In order to fix this error, open the model in XML editor and add the mapping of the PersonID manually.
The following XML is the model after the fix up:
="1.0" ="utf-8"
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
-->
<edmx:Runtime>
-->
<edmx:StorageModels>
<Schema Namespace="SchoolModel.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/02/edm/ssdl">
<EntityContainer Name="SchoolModelStoreContainer">
<EntitySet Name="Person" EntityType="SchoolModel.Store.Person"
store:Type="Tables" Schema="dbo" />
<EntitySet Name="PersonDateInfo" EntityType="SchoolModel.Store.PersonDateInfo"
store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_PersonDateInfo_Person"
Association="SchoolModel.Store.FK_PersonDateInfo_Person">
<End Role="Person" EntitySet="Person" />
<End Role="PersonDateInfo" EntitySet="PersonDateInfo" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonID" />
</Key>
<Property Name="PersonID" Type="int" Nullable="false"
StoreGeneratedPattern="Identity" />
<Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="FirstName" Type="nvarchar" Nullable="false" MaxLength="10" />
<Property Name="PersonType" Type="tinyint" Nullable="false" />
</EntityType>
<EntityType Name="PersonDateInfo">
<Key>
<PropertyRef Name="PersonID" />
</Key>
<Property Name="PersonID" Type="int" Nullable="false" />
<Property Name="PersonDate" Type="datetime" />
<Property Name="DateType" Type="tinyint" Nullable="false" />
</EntityType>
<Association Name="FK_PersonDateInfo_Person">
<End Role="Person" Type="SchoolModel.Store.Person" Multiplicity="1" />
<End Role="PersonDateInfo" Type="SchoolModel.Store.PersonDateInfo"
Multiplicity="0..1" />
<ReferentialConstraint>
<Principal Role="Person">
<PropertyRef Name="PersonID" />
</Principal>
<Dependent Role="PersonDateInfo">
<PropertyRef Name="PersonID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
-->
<edmx:ConceptualModels>
<Schema Namespace="SchoolModel" Alias="Self"
xmlns:annotation=http://schemas.microsoft.com/ado/2009/02/edm/annotation
xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="SchoolEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="People" EntityType="SchoolModel.Person" />
</EntityContainer>
<EntityType Name="Person" Abstract="true">
<Key>
<PropertyRef Name="PersonID" />
</Key>
<Property Name="PersonID" Type="Int32" Nullable="false"
annotation:StoreGeneratedPattern="Identity" />
<Property Name="LastName" Type="String" Nullable="false"
MaxLength="50" Unicode="true" FixedLength="false" />
<Property Name="FirstName" Type="String" Nullable="false"
MaxLength="10" Unicode="true" FixedLength="false" />
</EntityType>
<EntityType Name="Student" BaseType="SchoolModel.Person" >
<Property Type="DateTime" Name="EnrollementDate" Nullable="false" />
</EntityType>
<EntityType Name="Professor" BaseType="SchoolModel.Person" >
<Property Type="DateTime" Name="HireDate" Nullable="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
-->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="SchoolModelStoreContainer"
CdmEntityContainer="SchoolEntities">
<EntitySetMapping Name="People">
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Person)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Professor)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<Condition ColumnName="PersonType" Value="1" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo" >
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="HireDate" ColumnName="PersonDate" />
<Condition ColumnName="DateType" Value="1" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Student)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<Condition ColumnName="PersonType" Value="2" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo" >
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="EnrollementDate" ColumnName="PersonDate" />
<Condition ColumnName="DateType" Value="2" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
-->
<Designer xmlns="http://schemas.microsoft.com/ado/2008/10/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="True" />
</DesignerInfoPropertySet>
</Options>
-->
<Diagrams>
<Diagram Name="SchoolModel">
<EntityTypeShape EntityType="SchoolModel.Person" Width="1.5"
PointX="1.25" PointY="2.875" Height="1.5956835937499996" IsExpanded="true" />
<EntityTypeShape EntityType="SchoolModel.Student" Width="1.5"
PointX="4.25" PointY="5.375" Height="1.2110807291666665" />
<InheritanceConnector EntityType="SchoolModel.Student" >
<ConnectorPoint PointX="2" PointY="4.47068359375" />
<ConnectorPoint PointX="2" PointY="5" />
<ConnectorPoint PointX="5" PointY="5" />
<ConnectorPoint PointX="5" PointY="5.375" />
</InheritanceConnector>
<EntityTypeShape EntityType="SchoolModel.Professor" Width="1.5"
PointX="6.5" PointY="5.375" Height="1.2110807291666665" />
<InheritanceConnector EntityType="SchoolModel.Professor" >
<ConnectorPoint PointX="2" PointY="4.47068359375" />
<ConnectorPoint PointX="2" PointY="5" />
<ConnectorPoint PointX="7.25" PointY="5" />
<ConnectorPoint PointX="7.25" PointY="5.375" />
</InheritanceConnector>
</Diagram>
</Diagrams>
</Designer>
</edmx:Edmx>
Pay attention to the C-S mapping – the fix is located there. The code for the C-S mapping:
<EntitySetMapping Name="People">
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Person)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Professor)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<Condition ColumnName="PersonType" Value="1" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo" >
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="HireDate" ColumnName="PersonDate" />
<Condition ColumnName="DateType" Value="1" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(SchoolModel.Student)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<Condition ColumnName="PersonType" Value="2" />
</MappingFragment>
<MappingFragment StoreEntitySet="PersonDateInfo" >
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="EnrollementDate" ColumnName="PersonDate" />
<Condition ColumnName="DateType" Value="2" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
Step 5
Check the model.
The following code will query for the count of the students, professors and all people in the database:
using (var context = new SchoolEntities())
{
var students = context.People.OfType<Student>();
Console.WriteLine(students.ToTraceString());
var professors = context.People.OfType<Professor>();
Console.WriteLine(professors.ToTraceString());
var people = context.People.Count();
Console.WriteLine("students:{0} proffesors:{1} all:{2}",
students.Count(), professors.Count(), people);
}
The ToTraceString will output the query that is going to be sent to the database.
The result of the running of this program:
Summary
Let's sum up. I showed how to impose two model customization methods in one model – TPH and Entity Splitting. Customizing the model is a very common thing to do in order to create our needed conceptual model. As you could see, we can combine model customizations in order to achieve our need.
Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
My technical blog: http://www.gilfink.net