Click here to Skip to main content
11,649,360 members (76,107 online)
Click here to Skip to main content

Table Per Hierarchy Inheritance in Entity Framework

, 23 Jul 2010 CPOL 45.2K 19
Rate this:
Please Sign up or sign in to vote.
The article explains what Table Per Hierarchy Inheritance is and how to apply it in Entity Framework.

Introduction

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:

Database Schema

As you can see, the table holds two different fields from two different types: HireDate(which belong to a professortype) and EnrollmentDate(which belongs to studenttype). The table also includes an integer discriminator field which is called PersonType. When PersonTypeequals 1, the person type is a professorand when it is 2, the type is student.

TPH Example

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.

Step 1

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:

Entity Designer Diagram

We are going to use only the Personentity for the TPH demonstration.

Step 2

From the designer surface, use the Add –> Entity and add two entities: Professorand Student.

Add New Entities

Make Personentity their base type.

Add Professor

After that, the model should look like:

Entity Designer Diagram 1

Step 3

Move the HireDateproperty to the Professorentity and the EnrollmentDateproperty to the Studententity. Also remove the PersonTypeproperty from Personbecause it is going to be our discriminator field.

Entity Designer Diagram 2

Step 4

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:

Map The Student Entity

Step 5

Add a condition to the entities using the Mapping Details View. The condition should be on the PersonTypefield and should indicate that if PersonTypeequals 1, the person is a Professortype and if the PersonTypeequals 2, the persontype is a Student. The following figure shows how to do it in the Studenttype:
Add Condition

Step 6

Since 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 abstractflag to True.

Person Properties

Step 7

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
                select person;
    Console.WriteLine("All People: " + query.Count().ToString());

    var query1 = from student in context.People.OfType<Student>()
                 select student;
    Console.WriteLine("Students: " + query1.Count().ToString());

    var query2 = from proffesor in context.People.OfType<Professor>()
                 select proffesor;
    Console.WriteLine("Professors: " + query2.Count().ToString());

}

Summary

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.

History

  • 23rd July, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
zaman_cisco23-Oct-12 1:48
memberzaman_cisco23-Oct-12 1:48 
GeneralMy vote of 5 Pin
xjpepitceo13-Aug-12 23:35
memberxjpepitceo13-Aug-12 23:35 
GeneralModel first development Pin
Michael Ulmann1-Oct-10 20:36
memberMichael Ulmann1-Oct-10 20:36 
GeneralRe: Model first development Pin
Gil Fink1-Oct-10 22:06
memberGil Fink1-Oct-10 22:06 
GeneralReported TPT Performance Problem Pin
Jeff Bowman26-Jul-10 12:31
memberJeff Bowman26-Jul-10 12:31 
GeneralRe: Reported TPT Performance Problem Pin
Gil Fink26-Jul-10 20:07
memberGil Fink26-Jul-10 20:07 
GeneralRe: Reported TPT Performance Problem Pin
Jeff Bowman27-Jul-10 13:40
memberJeff Bowman27-Jul-10 13:40 
Frankly I'm surprised this made it out of Development, let alone Test. Production? Wow!
QuestionHow would the EF handle this with a true association table? [modified] Pin
Marc Clifton23-Jul-10 3:05
protectorMarc Clifton23-Jul-10 3:05 
AnswerRe: How would the EF handle this with a true association table? Pin
Pete O'Hanlon23-Jul-10 22:31
mvpPete O'Hanlon23-Jul-10 22:31 
AnswerRe: How would the EF handle this with a true association table? Pin
Gil Fink24-Jul-10 19:32
memberGil Fink24-Jul-10 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150804.4 | Last Updated 23 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid