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<Movie>().ToTable("Movies");
    modelBuilder.Entity<Cd>().ToTable("Cds");
    modelBuilder.Entity<Book>().ToTable("Books");
    base.OnModelCreating(modelBuilder);
}

Leave a Reply

Your email address will not be published. Required fields are marked *