Entity Framework Core Interview Question #159

What is Table-per-Hierarchy (TPH) mapping?

Advanced Mapping, Interceptors, Value Conversions & Architecture Senior Advanced

Quick Interview Answer

TPH stores an entire inheritance hierarchy in one table and uses a discriminator to identify the concrete type. It usually requires fewer joins and is EF Core's common inheritance approach, but the table can contain nullable columns that apply only to particular derived types.

Detailed Explanation

TPH stores an entire inheritance hierarchy in one table and uses a discriminator to identify the concrete type. It usually requires fewer joins and is EF Core's common inheritance approach, but the table can contain nullable columns that apply only to particular derived types.

Code Example

modelBuilder.Entity<Payment>()
    .HasDiscriminator<string>("PaymentType")
    .HasValue<CardPayment>("Card")
    .HasValue<CashPayment>("Cash");