Entity Framework Inheritance<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Entity Framework Inheritance

Posted on: December 10, 2013

Business logic can have model classes that inherit an other class. The data of the instanced class and the inherited class might be saved in the database when the Object Context is saved. Entity framework lets you choose the strategy you desire to save them into the database.

Three strategies are available : TPH, TPT and TPC inheritance.

Table per hierarchy (TPH) Inheritance Mapping Strategy

This is the simplest strategy. It takes all properties of the base class and all properties of the inherited class and merge them into a single table. This mean if 10 classes inherit a single class, all properties of these 10 classes plus the one of the inheritance one will be merged in 1 table.

For example you have 3 classes. Two sub-classes that inherit a single base class.

1public class Book { public string ISBN { get; set; } public int PageCount { get; set; } }
3public class Magazine : Book { public string MagazineProperty { get; set; } }
5public class Encyclopedia:Book { public string EncyclopediaProperty { get; set; } }

The class diagram of these 3 classes is the following:

ClassDiagram 400x318

This will result to a single table with all property nullable for those who are from subclass.

To create a short test, simply add these three classes to a .Net project. Then, you need to setup the database context.

1public class TestContext:DbContext {
2 public TestContext() : base("TestContext") {
3 }
4 public DbSet<Magazine> Magazines { get; set; }
5 public DbSet<Encyclopedia> Encyclopedia { get; set; }
7 protected override void OnModelCreating(DbModelBuilder modelBuilder) {
8 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
9 modelBuilder.Entity<Book>().HasKey(d => d.ISBN);
10 }

Do no forget to add Entity Framework with Nuget and setup the web.config with a good connection string. To get entity framework you can do a search inside Nuget manager or in the package console.

1Menu Project>Manage NuGet Packages Select the Online tab Select the EntityFramework package Click Install


1PM> install-package entityframework

The configuration file should have a config section (which are added from Nuget) and the connection string to be setup to the Sql instance you have and the database you want to use.

1<configuration> <configSections> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <connectionStrings> <add name="TestContext" connectionString="Data Source=PATRICK-I7\\SQLEXPRESS;Initial Catalog=TestContextDatabase;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> </connectionStrings> ...

This is the most basic setup possible, we simply set the primary key to the base class. We do not set anything for sub-classes . Then, we need to let Entity Framework create the database. For that, just instanciate the dbcontext and do something to the query. We could also generate the database with the migration tool but for simplicity, let's just add a new Encyclopedia in the main method.

1using (var context = new DataAccessLayer.TestContext()) {
2 context.Encyclopedia.Add(new Models.Encyclopedia { ISBN = "123" });
3 context.SaveChanges();

Here is how the table looks:


In fact, the table has an additional value which is a discriminator. This column is used by Entity Framework to know which sub class is required to be instanced. It's possible to edit the string, but by default it's the class name.

Discriminator 400x188

This mapping strategy is good for performance and simplicity. It's also the default used by Entity Framework.

Table per type (TPT) Inheritance Mapping Strategy

All types are mapped to its own table. This produce a normalized database, while TPH doesn't. For the same example as above, with TPH, the database would have 3 tables.


Entity Framework performs more inner join to be able to know which table to use for subclass when information are gathered from the database. This result to be less performing than the previous approach. Nevertheless, tables are more clean because they doesn't have a huge amount of nullable of property.

To configure table per type (TPT) it requires to set the table for the subclass.

1public class TestContext : DbContext {
2 public TestContext() : base("TestContext") { }
4 public DbSet<Book> Book { get; set; }
5 public DbSet<Magazine> Magazines { get; set; }
6 public DbSet<Encyclopedia> Encyclopedia { get; set; }
8 protected override void OnModelCreating(DbModelBuilder modelBuilder) {
9 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
10 modelBuilder.Entity<Magazine>().ToTable("Magazine");
11 modelBuilder.Entity<Encyclopedia>().ToTable("Encyclopedia");
12 modelBuilder.Entity<Book>().ToTable("Book").HasKey(d => d.ISBN);
13 }

If we delete the database and run again the code, the database is created with two tables.

Table per concrete type (TPC) Inheritance Mapping Strategy

Tablet per concrete type, also know as TPC, create a single class per subclass. Every properties of the inherited class are set into each of the subclass table. This lead to have duplicated structure. If you have 2 classes that inherit one class, the inherited field will be in inside the two tables.


As you can see in the example above, the ISBN and PageCount are repeated in both of the sub classes' table. You can also notice that they are no table for the base class. The reason is that all fields of the base are already in all sub classes.

The configuration is more complex.

1public class TestContext:DbContext {
2 public TestContext() : base("TestContext") {
3 }
5 public DbSet<Magazine> Magazines { get; set; }
6 public DbSet<Encyclopedia> Encyclopedia { get; set; }
8 protected override void OnModelCreating(DbModelBuilder modelBuilder) {
9 modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
10 modelBuilder.Entity<Magazine>().Map(m => { m.MapInheritedProperties(); m.ToTable("Magazine");
11 });
13 modelBuilder.Entity<Encyclopedia>().Map(m => {
14 m.MapInheritedProperties();
15 m.ToTable("Encyclopedia");
16 });
18 modelBuilder.Entity<Magazine>().HasKey(d => d.ISBN);
19 modelBuilder.Entity<Encyclopedia>().HasKey(d => d.ISBN);
20 }

The code generated in SQL is complex. It has sub query with union to know from which table to take its data. This result to have bad performance for heavy inheritance model. TPC is good for deep inheritance, TPH is good for small entity with not a lot of property and TPT is good for small inheritance level with many properties. Personally, I lean for TPT most of the time. Having a normalize database is important in most enterprise and performance is not that bad if the inheritance is only one level.