dotnet/efcore · error · InvalidOperationException
The table '{table}' cannot be used for entity type '{entityT
Error message
The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and there is a relationship between their primary keys in which '{entityType}' is the dependent, but '{entityType}' has a base entity type mapped to a different table. Either map '{otherEntityType}' to a different table, or invert the relationship between '{entityType}' and '{otherEntityType}'. What it means
In shared-table mapping, a dependent entity is linked to its principal via a foreign key on the primary key. If that dependent also has a base entity type (inheritance) that is mapped to a different table, the sharing is inconsistent — the dependent's row cannot live in both tables. The validator at line 1172-1179 detects this when it finds a linking FK to another shared type but the current type already has a base type mapped elsewhere.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1174
var unvalidatedTypes = new HashSet<IEntityType>(mappedTypes);
IEntityType? root = null;
foreach (var mappedType in mappedTypes)
{
if (mappedType.BaseType != null && unvalidatedTypes.Contains(mappedType.BaseType))
{
continue;
}
var primaryKey = mappedType.FindPrimaryKey();
if (primaryKey != null
&& (mappedType.FindForeignKeys(primaryKey.Properties)
.FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()
&& !fk.PrincipalEntityType.IsAssignableFrom(fk.DeclaringEntityType)
&& unvalidatedTypes.Contains(fk.PrincipalEntityType)) is { } linkingFK))
{
if (mappedType.BaseType != null)
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleTableDerivedRelationship(
table.DisplayName(),
mappedType.DisplayName(),
linkingFK.PrincipalEntityType.DisplayName()));
}
continue;
}
if (root != null)
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleTableNoRelationship(
table.DisplayName(),
mappedType.DisplayName(),
root.DisplayName()));
}
View on GitHub (pinned to dbf9771522)
Solutions
- Map the principal entity (otherEntityType) to a different table so the dependent's inheritance chain and sharing do not conflict.
- Invert the relationship so the entity currently marked dependent becomes the principal, removing the FK-on-PK dependency.
- Unify the table mapping: ensure the dependent's base type is also mapped to the same table (or break the inheritance link).
Example fix
// before: Detail derives from AuditEntity (mapped to 'audits')
// but shares table 'Orders' with Order via PK FK
modelBuilder.Entity<Order>().OwnsOne(o => o.Detail);
modelBuilder.Entity<AuditEntity>().ToTable("audits");
// after: map Order to a distinct table so no conflict
modelBuilder.Entity<Order>().ToTable("Orders");
modelBuilder.Entity<Order>().OwnsOne(o => o.Detail);
// and ensure Detail's base is not also mapped to 'Orders' Defensive patterns
Strategy: validation
Validate before calling
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
if (et.BaseType == null) continue;
var table = et.GetTableName();
if (table == null) continue;
var baseTable = et.BaseType.GetTableName();
if (table == baseTable) continue; // consistent
var pk = et.FindPrimaryKey();
if (pk == null) continue;
var linkingFk = et.FindForeignKeys(pk.Properties)
.FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()
&& !fk.PrincipalEntityType.IsAssignableFrom(et)
&& fk.PrincipalEntityType.GetTableName() == table);
if (linkingFk != null)
throw new InvalidOperationException(
$"{et.Name} maps table '{table}' with a linking FK but its base maps '{baseTable}'.");
} Prevention
- Keep inheritance and table-sharing orthogonal: a derived type that shares a table should have its base on the same table.
- Avoid TPT-style mappings combined with owned-type table splitting.
- Review ToTable calls on every type in an inheritance hierarchy after refactoring.
When it happens
Trigger: Combining inheritance (TPH/TPC/TPT) with table splitting such that a derived/owned entity has both a base type on a different table and an identifying FK to a principal on the shared table. Triggered during ValidateSharedTableCompatibility traversal at line 1158-1194.
Common situations: Mixing TPT (table-per-type) with owned entity table sharing; refactoring an inheritance hierarchy so a type that used to share now has a base elsewhere; misconfigured ToTable on an owned derived type.
Related errors
- '{entityType1}.{property1}' and '{entityType2}.{property2}'
- Trigger '{trigger}' is defined on entity type '{entityType}'
- The entity type '{entityType}' is mapped to the 'DbFunction'
- Both entity type '{entityType1}' and '{entityType2}' were co
- Entity type '{entityType}' is an optional dependent using ta
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/c0058855d5bd3240.
Report an issue: GitHub.