Click here to Skip to main content
14,360,787 members

Entity Framework Code First Inheritance: Table Per Hierarchy and Table Per Type

Rate this:
4.90 (9 votes)
Please Sign up or sign in to vote.
4.90 (9 votes)
5 Jun 2012CPOL
Entity Framework Code First Inheritance: Table Per Hierarchy and Table Per Type

Recently, I answered a question in stackoverflow about how to create separate tables for each sub type when handling inheritance. Today, I thought of expanding it with some sample source code and share it here.

While working with data driven applications, sometimes we need to implement Inheritance in our code as well as the data storage. Let’s take a look at how Entity Framework CodeFirst handles this.

Table Per Hierarchy (TPH)

By default, Entity framework code first uses Table Per Hierarchy method to handle inheritance when creating tables from entities. In TPH, All Data in the hierarchy will be saved in a single database table and it uses a Discriminator column to indentify which record belongs to which sub type. The value of this column will be the name of the subtype.

Let's take an example.

I have a base class called Vehicle from where I am inheriting a Car class and a MotorCycle class.

Image 1

So, CodeFirst will create a table like this. All the properties in the inheritance hierarchy is in the same table with the Discriminator field.

Image 2

If you look at the data (after adding some via the application), it will be like this. You can see that the value of Discriminator column is the name of the type.

Image 3

The records with Discriminator value as “Car” are the records for Car type and the records with Discriminator value as “Motorcycle” are for Motorcycle entity. Note the HandleBarColor is null for the first two records because those records are for the Car type and we don’t have a property for the Car type in our inheritance hierarchy. So it became NULL.

Table Per Type (TPH)

In Table per Type method, Entity Framework CodeFirst will create a table for the base class (with the base class properties as the columns) and individual tables for each subtype in the hierarchy. To do this, we can override the OnModelCreating method of DBContext and write some fluent API Code.

Image 4

As a result, now our tables will be like this:

Image 5

Image 6

Image 7

And the data will look like this:

Image 8

Note the ID value of the Derived type tables are same as the Identity column value of the base type record.

I hope this gives you some basic idea about how entity framework handles inheritance when doing code first way of development. You can download the sample source code which I used in the post here. Don’t forget to leave me a comment, if this post helps you.


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


About the Author

Software Developer (Senior)
United States United States
Shyju is a curious developer who loves to build highly scalabale and robust software on top of Microsoft development stack. He is a strong believer in test driven development and occasionally writes Tips & Tricks on ASP.NET MVC, Entity Framework, jQuery in his technical blog

Twitter : kshyju
Blog : TechiesWeb

Comments and Discussions

QuestionMisssing images and info - link is here Pin
martinrj307-Sep-15 19:14
membermartinrj307-Sep-15 19:14 
SuggestionTable Per Type (TPH) Pin
Aditya Thakur17-Jul-14 0:24
memberAditya Thakur17-Jul-14 0:24 
QuestionThanks a lot! One question though - Pin
shay_e9-May-13 23:22
membershay_e9-May-13 23:22 
Questiongood and easily understood article Pin
v0305143526-Apr-13 0:18
memberv0305143526-Apr-13 0:18 
QuestionCode First Pin
Sponaris3-Dec-12 5:03
memberSponaris3-Dec-12 5:03 
AnswerRe: Code First Pin
kshyju3-Dec-12 5:05
memberkshyju3-Dec-12 5:05 
Generalmy vote of 5 Pin
inf1n1te22-Aug-12 4:42
professionalinf1n1te22-Aug-12 4:42 
GeneralMy vote of 4 Pin
BeeWayDev2-Aug-12 8:35
memberBeeWayDev2-Aug-12 8:35 

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

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

Technical Blog
Posted 5 Jun 2012


9 bookmarked