Benny Michielsen

Entity Framework 4.1 Inheritance – Table per Type

Using the same model as before, we want to store it more in a normalized fashion and in my opinion something that closely resembles a typical inheritance structure in code.



Lets create one table for each entity with only the properties it contains, all other ones are linked through a one to one mapping. To achieve this result you’ll need to create your mappings as illustrated below.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity().ToTable("Movies");
    modelBuilder.Entity().ToTable("Cds");
    modelBuilder.Entity().ToTable("Books");
    base.OnModelCreating(modelBuilder);
}