Inheritance: TPH, TPT and TPC
When your classes use inheritance, EF Core must decide how to store them in tables. This lesson explains the three strategies, table per hierarchy, table per type and table per concrete type, with the trade-offs in speed and table design.
- TPHOne table + discriminator; fastest; nullable subtype columns
- TPTBase table + one per subtype; joins on every query
- TPCOne full table per concrete type; UNION ALL for the base
- Start withTPH, and change only for a measured reason
The example
A payments system has a base class Payment and two kinds: CardPayment and UpiPayment, each with its own fields. Queries such as db.Payments.ToList() should return both kinds.
public abstract class Payment
{
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime PaidAt { get; set; }
}
public class CardPayment : Payment { public string Last4 { get; set; } = ""; }
public class UpiPayment : Payment { public string UpiId { get; set; } = ""; }
public DbSet<Payment> Payments => Set<Payment>();The three strategies
- TPH (table per hierarchy, the default): one Payments table with every column and a Discriminator column. Fastest queries, no joins; subclass columns must be nullable.
- TPT (table per type): a Payments table plus CardPayments and UpiPayments tables joined by key. Clean, normalized tables, but every query needs joins and gets slow with many types.
- TPC (table per concrete type, EF Core 7+): one full table per concrete class, no base table. No joins for one type; querying the base type uses UNION ALL. Keys must be unique across tables, so use a sequence or GUIDs.
Configuring them
// TPH (default) with a nicer discriminator
modelBuilder.Entity<Payment>()
.HasDiscriminator<string>("Method")
.HasValue<CardPayment>("card")
.HasValue<UpiPayment>("upi");
// TPT
modelBuilder.Entity<Payment>().UseTptMappingStrategy();
// TPC
modelBuilder.Entity<Payment>().UseTpcMappingStrategy();
// Query only one type
var upi = await db.Payments.OfType<UpiPayment>().ToListAsync();Choosing
Start with TPH. It is the fastest and simplest, and nullable subclass columns are rarely a real problem. Choose TPC when you usually query one concrete type at a time and want separate tables. Avoid TPT for hot paths; its joins cost the most.
Interview question
What are TPH, TPT and TPC in EF Core, and which performs best?
They are ways to map an inheritance hierarchy. TPH stores all types in one table with a discriminator; TPT stores the base in one table and each subtype in its own table joined by key; TPC stores each concrete type in its own complete table. TPH is usually fastest because it needs no joins or unions; TPC is good when querying single types; TPT is usually slowest.
Want the full details? Read the official Microsoft documentation for this topic.