Introduction
One of the interesting features of Code First fluent API is the ability
to configure inheritance in your model. Since one of the strengths of an O/RM solution is its ability to map inheritance, this feature is a must in every mapping scenario (may it be Model first, Database First or Code First). In this post, I'll show how to configure inheritance by using the fluent API.
Revisiting Entity Framework Inheritance Types
In the past, I wrote a series of posts about inheritance mapping in Entity Framework which you can read here:
Configure Inheritance Using Code First Fluent API
In the previous CTPs, in order to create inheritance we had to use the MapHierarchy method which was very tedious. In this version, the MapHierarchy method was replaced by the Map method (and not in all the inheritance mapping scenarios you'll need to use it). Let's explore the inheritance types:
Table Per Type (TPT) Inheritance
In order to create this type of mapping, all you have to do is to use the ToTable method:
public class Course
{
#region Properties
public int CourseID { get; set; }
public string Title { get; set; }
public string Days { get; set; }
public DateTime Time { get; set; }
public string Location { get; set; }
public int Credits { get; set; }
#endregion
}
public class OnLineCourse : Course
{
#region Properties
public string CourseURL { get; set; }
#endregion
}
public class SchoolEntities : DbContext
{
#region Properties
public DbSet<Course> Courses { get; set; }
#endregion
#region Methods
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.ToTable("Courses");
modelBuilder.Entity<OnLineCourse>()
.ToTable("OnLineCourses");
}
#endregion
}
You start with creating the inheritance in the model itself (OnLineCourse inherits from Course) and then in the DbContext’s OnModelCreating method, you specify that every type will be mapped to its own table using the ToTable method.
Table Per Hierarchy (TPH) Inheritance
In order to create this type of mapping, you need to use the Map method. In the lambda, the Map method gets you write the discriminator field and its values using the Requires and HasValue methods. In the following example, you can see how to do exactly that:
public class Course
{
#region Properties
public int CourseID { get; set; }
public string Title { get; set; }
public string Days { get; set; }
public DateTime Time { get; set; }
public string Location { get; set; }
public int Credits { get; set; }
#endregion
}
public class OnLineCourse : Course
{
#region Properties
public string CourseURL { get; set; }
#endregion
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.Map(c =>
{
c.Requires("Discriminator").HasValue(1);
})
.Map<OnLineCourse>(olc =>
{
olc.Requires("Discriminator").HasValue(2);
});
}
Table Per Concrete Type (TPC) Inheritance
In order to create this type of mapping, you will start as it is a table per type inheritance configuration. The only change is the use of the Map method on the concrete type. In the method lambda, you will use the MapInheritedProperties method to indicate it is a concrete type:
public class Course
{
#region Properties
public int CourseID { get; set; }
public string Title { get; set; }
public string Days { get; set; }
public DateTime Time { get; set; }
public string Location { get; set; }
public int Credits { get; set; }
#endregion
}
public class OnLineCourse : Course
{
#region Properties
public string CourseURL { get; set; }
#endregion
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Course>()
.ToTable("Courses");
modelBuilder.Entity<OnLineCourse>()
.Map(olc =>
{
olc.MapInheritedProperties();
}).ToTable("OnLineCourses");
}
Here is a simple check to see how the database is created in each inheritance type:
using (SchoolEntities context = new SchoolEntities())
{
context.Database.Delete();
var course = new Course
{
Credits = 2,
Days = "MF",
Location = "Class 1",
Time = DateTime.Now,
Title = "Entity Framework",
};
var onLineCourse = new OnLineCourse
{
Credits = 3,
Days = "WT",
Location = "Class 2",
Time = DateTime.Now,
Title = "Entity Framework On Line",
CourseURL = "http://www.gilfink.net",
};
context.Courses.Add(course);
context.Courses.Add(onLineCourse);
context.SaveChanges();
Pay attention that in the TPC inheritance, the database will be created properly and the insert will succeed but there will be an exception of overlapping keys (both courses will be created with CourseID of 1 and in the context there will be overlapping).
Summary
Using the Code First fluent API for inheritance is easy. In the post, I showed how you can do that using the three main inheritance scenarios in Entity Framework.


Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
My technical blog: http://www.gilfink.net