Entity Framework Code First Inheritance : Table Per Type and Table Per Hierarchy
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, Some times 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.
Le’ts take an example.
I have a base class called Vehicle from where I am inheriting a Car class and a MotorCycle class.
So CodeFirst will create a table like this. All the properties in the inheritance hierarchy is in the same table with the Discriminator field.
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.
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.
As a result, Now our tables will be like this.
And the data will look like this
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.









May 28th, 2012 at 11:29 am
Nice write up. I haven’t messed with any EF stuff in the past 6 months as I have a DBA doing all the heavy lifting for me. But this could be handy for personal projects. Good to know this otherwise I’d see everything in a single table and would start cursing up a storm
October 20th, 2012 at 3:55 pm
Ufff esto es justo lo que estaba buscando!!! Muchas gracias por el post, muy claro y facil de entender.
November 8th, 2012 at 2:18 pm
really appreciated. One more question I have two entities Subjects and Courses and used property
public Subject Subject { get; set; }
in Course but not inheriting.Even it is creating Discriminator column in Subjects entity. Please reply.
November 8th, 2012 at 2:23 pm
Shamim,
Can you post your model classes (with only relevant properties) ? I will take a look.
November 8th, 2012 at 2:26 pm
Shyju,
Please find below Course and Subject classes
public class Course
{
public int Id { get; set; }
public string Name { get; set; }
public string Remarks { get; set; }
public int SubjectId { get; set; }
public Subject Subject { get; set; }
}
public class Subject
{
public int Id { get; set; }
public string Name { get; set; }
public string Remarks { get; set; }
public string Source { get; set; }
}
November 8th, 2012 at 2:49 pm
Shamim,
Your classes are straight forward. It will not create Discriminator. I tried your code and it created 2 table as per the model classes.
Here is the screen shot of the table structure i got with the classes you provided.
Can you create the same classes in a new project ? You may download the source code from this article and add your 2 classes there and see what happens. If you still face the problem. Share that source code. I will look into that.
Let me know how it goes.
November 8th, 2012 at 8:21 pm
Shyju,
It is creating Discriminator with vs2012 with LocalDb. I tried with vs2010 it is fine.
November 9th, 2012 at 1:13 am
Shamim,
I tried it in VS2010. I will try in VS2012 and let you know.
By the way did you get an email notification about my reply ?
November 9th, 2012 at 1:35 am
Shamim, I created a new project in 2012, which has reference to EF5. I have an SQL Express instance in my machine and EF created tables as earlier in my SQL Express database.
I think there is something else giving you trouble. Are you overriding the
onModelCreatingmethod ? What are you doing inside that ? Did you try to create it in a new project with only the relevant code/ classes.November 9th, 2012 at 7:13 am
Shyju,
Sure I will try it. I did not override onModelCreating.
I did not get any emial notification. Thanks dear.
November 11th, 2012 at 6:05 am
Shyju,
I tried with new project in vs2012. It is fine and not creating Discriminator column.
Thanks.