skip to content

In EF Core 10, what tables do UseTptMappingStrategy and UseTpcMappingStrategy create for a Payment hierarchy, and how do the schemas differ?

level: middleimportance: should knowfreq 42%

answer

  1. called once, on the root type
  2. TPT: base table plus a table per subclass
  3. derived key is also a foreign key
  4. TPC: no table for an abstract base
  5. TPC repeats base columns in every table

basics

~20 s

Both are called on the root entity. TPT creates a Payments table for base columns plus a table per subclass whose key is also a foreign key to Payments. TPC creates one complete table per concrete class, repeating the base columns, and none for an abstract Payment.

solid answer

~40 s

`modelBuilder.Entity<Payment>().UseTptMappingStrategy()` gives every type its own table: `Payments` holds `Id`, `Amount` and `CreatedAt`, and `CardPayments`, `BankTransferPayments` and `VoucherPayments` hold only their own columns plus an `Id` that is both their primary key and a foreign key to `Payments`. One card payment is two rows. `UseTpcMappingStrategy()` instead creates a table per **concrete** type with every column it needs, inherited ones included, and no table for the abstract `Payment`; one payment is one row. Neither has a discriminator column: TPT works out the type from which joined table has a row, TPC from which table the row came from. Table names come from the `DbSet` names or `ToTable`. TPC's catch is that a foreign key from another table to `Payment`, such as a refund's `PaymentId`, cannot get a database constraint.

code

csharp · 9 lines
csharp
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // pick one per hierarchy, always on the root type
    modelBuilder.Entity<Payment>().UseTpcMappingStrategy();

    modelBuilder.Entity<CardPayment>().ToTable("CardPayments");
    modelBuilder.Entity<BankTransferPayment>().ToTable("BankTransferPayments");
    modelBuilder.Entity<VoucherPayment>().ToTable("VoucherPayments");
}

go deeper

for a junior

Recall the two calls, UseTptMappingStrategy and UseTpcMappingStrategy, made on the root type, and that TPT has a table per type while TPC has one per concrete type.

for a middle

Draw both schemas: shared key plus foreign key in TPT, repeated base columns in TPC, no discriminator in either, and how many rows one payment takes.

for a senior

Weigh the schema consequences - TPT's joins and split indexes, TPC's lost foreign key constraints - before a hierarchy is shipped.

for a principal

Decide how much database-enforced integrity a team gives up for TPC's single-table rows, and who owns the data migration when the strategy changes.

## Choosing a strategy in code EF Core maps an inheritance hierarchy with table-per-hierarchy (TPH) unless you pick another strategy on the **root** entity type: - `modelBuilder.Entity<Payment>().UseTptMappingStrategy()` for **table-per-type** (available since EF Core 5); - `modelBuilder.Entity<Payment>().UseTpcMappingStrategy()` for **table-per-concrete-type** (available since EF Core 7); - `UseTphMappingStrategy()` states the default explicitly. Each type's table name comes from its `DbSet` property name on the context, or from `ToTable`/`[Table]`. Before these methods existed, TPT was configured by calling `ToTable` with a different name on every type, and that still works. The billing hierarchy used below is an abstract `Payment` (`Id`, `Amount`, `CreatedAt`) with `CardPayment` (`CardBrand`, `CardLast4`), `BankTransferPayment` (`Iban`) and `VoucherPayment` (`VoucherCode`). ## Table-per-type (TPT) TPT mirrors the class diagram: one table per type, abstract or not, each holding only the properties declared on that type. | Table | Columns | |---|---| | `Payments` | `Id` (generated here), `Amount`, `CreatedAt` | | `CardPayments` | `Id` (PK and FK to `Payments.Id`), `CardBrand`, `CardLast4` | | `BankTransferPayments` | `Id` (PK and FK), `Iban` | | `VoucherPayments` | `Id` (PK and FK), `VoucherCode` | Consequences: - saving one card payment inserts a row in `Payments` and a row in `CardPayments`; - loading it joins the two; - there is no discriminator column: EF Core infers the type from which subtype table has a matching row; - subclass columns can be `NOT NULL`, because only rows of that type live in the table; - because inherited and declared columns sit in different tables, an index or composite foreign key that combines them cannot be created. ## Table-per-concrete-type (TPC) TPC creates a table for every type that can have instances, and each table is complete on its own: | Table | Columns | |---|---| | `CardPayments` | `Id`, `Amount`, `CreatedAt`, `CardBrand`, `CardLast4` | | `BankTransferPayments` | `Id`, `Amount`, `CreatedAt`, `Iban` | | `VoucherPayments` | `Id`, `Amount`, `CreatedAt`, `VoucherCode` | Consequences: 1. The abstract `Payment` gets **no table**: C# cannot create one, so there is nothing to store. Explicitly mapping an abstract type to a table under TPC is rejected by model validation. If `Payment` were concrete, a `Payments` table would hold plain payments. 2. Base columns are **repeated** in every table; the schema is deliberately denormalized. 3. One payment is **one row in one table**, so inserts and single-type reads touch one table. 4. There is no discriminator column; the table a row comes from tells EF Core its type. 5. Keys must be unique across all the tables, so a plain identity column per table does not work; EF Core uses a shared database sequence by default. ## The foreign-key catch Another entity often points at the hierarchy, such as a `Refund` with a `PaymentId`: - under TPH and TPT, `PaymentId` can carry a normal foreign key constraint to `Payments.Id`, because every payment has a row there; - under TPC there is no single table holding every payment's key, so the database cannot enforce that reference. EF Core reports this at model validation with `RelationalEventId.ForeignKeyTpcPrincipalWarning` and creates no constraint; data written through navigations stays consistent, but the database no longer checks it. ## What does not change The strategy is a mapping decision, invisible to the rest of the code: - the classes, `DbSet` properties and navigations stay exactly as they were; - LINQ such as `db.Payments.Where(p => p.Amount > 100)` and `db.Set<CardPayment>()` is written the same way; - change tracking, `Add` and `SaveChanges` behave the same from the caller's point of view. What differs is the SQL EF Core generates for those calls and the schema it expects underneath, which is why the choice is about query cost and data integrity rather than code style. ## Changing strategy later Switching an existing hierarchy between TPH, TPT and TPC changes table shapes, not just configuration. Read the migration EF Core generates before applying it: rows have to move between tables, and that data movement is your job to plan. ## Quick comparison | | TPH | TPT | TPC | |---|---|---|---| | Tables | 1 | one per type | one per concrete type | | Discriminator column | yes | no | no | | Subclass columns nullable | yes | no | no | | Rows per entity | 1 | one per level | 1 | | FK constraint to the base | yes | yes | no |

  • Why does saving one CardPayment write two rows under TPT but one under TPC?
    TPT splits an entity's columns by the type that declares them, so `Amount` and `CreatedAt` go to `Payments` and `CardLast4` goes to `CardPayments`, linked by the shared key. TPC stores every column of a concrete type in that type's table, inherited ones included, so the whole card payment is one row in `CardPayments`.
  • Why can't a Refund.PaymentId column have a foreign key constraint when payments use TPC?
    A constraint must reference one table, but a TPC payment's key lives in `CardPayments`, `BankTransferPayments` or `VoucherPayments` depending on its type. No table holds every payment key, so EF Core logs `ForeignKeyTpcPrincipalWarning` and creates no constraint. Relationships saved through navigations stay correct; the database just cannot check them.

  • UseTptMappingStrategy must be called on every subclass, not the root.
  • TPC creates a Payments table for the abstract base class.
  • TPT keeps a Discriminator column alongside the per-type tables.
  • Under TPC, other tables can hold a normal foreign key constraint to all payments.
  • Under TPT each subclass table repeats Amount and CreatedAt.