In the second inheritance mappings tutorial, I’m going to write about the Table Per Hierarchy (TPH) inheritance mapping. If you want to read about the first mapping I showed, go to the Table Per Type post from here.
Table Per Hierarchy Definition
In TPH, the inheritance tree is created through one table only.
The TPH inheritance depends on a conditional mapping which is defined by a condition such as a discriminator database field. The condition is used to define records as different types. For example, in the following database schema the
Persontable includes a TPH inheritance:
As you can see, the table holds two different fields from two different types:
HireDate(which belong to a
EnrollmentDate(which belongs to
studenttype). The table also includes an integer discriminator field which is called
1, the person type is a
professorand when it is
2, the type is
The following steps will help you to understand how you can create a TPH inheritance mapping. I’m going to use the exact database from the first figure I showed.
The first step is to create the Entity Data Model from the database. Here is the EDM I’m going to use in the example:
We are going to use only the
Personentity for the TPH demonstration.
From the designer surface, use the Add –> Entity and add two entities:
Personentity their base type.
After that, the model should look like:
HireDateproperty to the
Professorentity and the
EnrollmentDateproperty to the
Studententity. Also remove the
Personbecause it is going to be our discriminator field.
In the Mapping Details View map the
Studententity and the
Professorentity to the
Persontable. Also map the relevant properties to the fields in the database. The following figure shows how to map the student entity:
Add a condition to the entities using the Mapping Details View. The condition should be on the
PersonTypefield and should indicate that if
1, the person is a
Professortype and if the
persontype is a
Student. The following figure shows how to do it in the
Personis a base type which is an
abstracttype, we need to indicate that it is
abstract. Point on the
Personentity and press F4 to open its properties dialog. In the dialog, turn the
Test the inheritance mapping. The following code will print the number of people in the database and also the number of professors and students:
using (var context = new SchoolEntities())
var query = from person in context.People
Console.WriteLine("All People: " + query.Count().ToString());
var query1 = from student in context.People.OfType<Student>()
Console.WriteLine("Students: " + query1.Count().ToString());
var query2 = from proffesor in context.People.OfType<Professor>()
Console.WriteLine("Professors: " + query2.Count().ToString());
Let's sum up. In the article, I explained what is Table Per Hierarchy inheritance and showed how we can create that inheritance in a specific model. The use of TPH is common and sometimes can lead to very big tables which include a lot of fields. Since it is a bad habit to create very big tables, I suggest to use TPH with TPT.
- 23rd July, 2010: Initial post