Entity Framework 4.1 Inheritance – Table per Concrete Type

There’s one more way to implement the sample domain model in the database and that’s a table for each class with all properties. And unfortunatly our luck ends a bit here. Although it’s possible to generate an ERD that looks like what we want to achieve I need to change the domain model.

You get the database model above by using the following mapping code

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Movie>()
        .Map(x =>  x.MapInheritedProperties())
        .ToTable("Movies");
    modelBuilder.Entity<Cd>()
        .Map(x => x.MapInheritedProperties())
        .ToTable("Cds");
    modelBuilder.Entity<Book>()
        .Map(x=> x.MapInheritedProperties())
        .ToTable("Books");
 
    base.OnModelCreating(modelBuilder);
}

Pollymorphic associations though are not supported using this inheritance strategy and so you need to omit the collection of products in the category class, bummer.

public class Category
{
    [Required]
    public virtual Guid Id { get; set; }
    [Required]
    public virtual string Name { get; set; }
    //public virtual IList<Product> Products { get; set; }
}

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.