3

I have two entities in my project Student and Teacher which share a common base class, AccountModel. The base class contains properties that are required by both students and teachers (semantically, both students and teachers are account holders, and for good practice, this prevents violation of the DRY principle)

In my Fluent API config I have:

builder
    .Ignore<AccountModel>();

builder
    .Entity<Student>()
    .HasBaseType<AccountModel>()
    .ToTable("Students");

builder
    .Entity<Teacher>()
    .HasBaseType<AccountModel>()
    .ToTable("Teachers");

But when EF scaffolds a migration and generates a new database, I get an AccountModel table, but not a Students or Teachers table. What gives?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • According to the [documentation](https://learn.microsoft.com/en-us/ef/core/modeling/relational/inheritance), currently EF Core supports only TPH pattern, hence the database table you get. I have no idea what's the purpose of `HasBaseType`. – Ivan Stoev Dec 16 '16 at 12:32
  • @IvanStoev what is the TPH pattern? – Matthew Layton Dec 16 '16 at 12:33
  • Single table containing all descendant fields plus "discriminator" column :) – Ivan Stoev Dec 16 '16 at 12:34
  • Similar to https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph – Ivan Stoev Dec 16 '16 at 12:35

1 Answers1

6

Currently, Entity Framework Core only supports the Table Per Hierarchy (TPH) pattern for mapping inheritance (http://www.learnentityframeworkcore.com/inheritance), which is why the migration results in one table for all types.

Table per concrete type (TPC) is on the backlog and is actively being considered, as is the Table Per Type (TPT) pattern.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88