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?
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.