Why is a type discriminator not supported in TPT mapping strategy? #35286
Closed as not planned
Description
opened on Dec 6, 2024
While configuring an EF Core entity using Table-per-Type (TPT) mapping strategy, I attempted to set up a type discriminator for easier identification of the derived entity types. However, it seems this feature is not supported when using TPT. Below is the configuration code I used:
modelBuilder.Entity<MenuItemEntity>()
.ToTable("MenuItem", Schema)
.UseTptMappingStrategy()
.HasDiscriminator<MenuItemType>("Type")
.HasValue<FileMenuItemEntity>(MenuItemType.File)
.HasValue<FolderMenuItemEntity>(MenuItemType.Folder)
.HasValue<FormElementMenuItemEntity>(MenuItemType.FormElement)
.HasValue<PluginMenuItemEntity>(MenuItemType.Plugin)
.HasValue<PowerBIMenuItemEntity>(MenuItemType.PowerBI)
.HasValue<ReportMenuItemEntity>(MenuItemType.Report)
.HasValue<UrlMenuItemEntity>(MenuItemType.Url);
Upon running the application, the configuration fails, and after reviewing the documentation, it seems TPT does not allow a discriminator column.
System.InvalidOperationException: The mapping strategy 'TPT' specified on 'MenuItemEntity' is not supported for entity types with a discriminator.
This raises the following questions:
- Why is a discriminator column not supported when using the TPT mapping strategy? I want to have this discriminator column at
MenuItemEntity
for SQL reports outside my app. - Are there any underlying technical limitations or design decisions behind this?
Understanding the rationale behind this limitation would be helpful for designing better solutions or workarounds for applications that need TPT with type discriminators.
Activity