Fluent API Table Scaffolding - HasBaseType

c# ef-fluent-api entity-framework entity-framework-core fluent

Question

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?

1
2
12/16/2016 12:18:28 PM

Accepted Answer

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.

3
12/19/2016 7:47:48 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow