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 the name '{keyName}' of the primary key {primaryKey} does not match the name '{otherName}' of the primary key {otherPrimaryKey}. What it means
All entity types sharing a table must agree on the primary key constraint name so that migrations emit a single, consistent PK. The validator at line 1217-1228 compares `key.GetName(table)` between connected types during the BFS traversal (line 1212-1228) and throws when the names differ, reporting both names and property lists.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1219
while (typesToValidate.Count > 0)
{
var entityType = typesToValidate.Dequeue();
var key = entityType.FindPrimaryKey();
var comment = entityType.GetComment();
var isExcluded = entityType.IsTableExcludedFromMigrations(table);
var typesToValidateLeft = typesToValidate.Count;
var directlyConnectedTypes = unvalidatedTypes.Where(unvalidatedType =>
entityType.IsAssignableFrom(unvalidatedType)
|| IsIdentifyingPrincipal(unvalidatedType, entityType));
foreach (var nextEntityType in directlyConnectedTypes)
{
if (key != null)
{
var otherKey = nextEntityType.FindPrimaryKey()!;
if (key.GetName(table) != otherKey.GetName(table))
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleTableKeyNameMismatch(
table.DisplayName(),
entityType.DisplayName(),
nextEntityType.DisplayName(),
key.GetName(table),
key.Properties.Format(),
otherKey.GetName(table),
otherKey.Properties.Format()));
}
}
var nextComment = nextEntityType.GetComment();
if (comment != null)
{
if (nextComment != null
&& !comment.Equals(nextComment, StringComparison.Ordinal))
{
throw new InvalidOperationException(View on GitHub (pinned to dbf9771522)
Solutions
- Make the key name identical on all entity types sharing the table: call .HasKey(...).HasName("<sameName>") on each, or remove explicit names so the default is computed consistently.
- Verify there are not two different .HasName() calls producing the mismatch shown in the error.
- If the names must differ for legacy reasons, map the types to different tables.
Example fix
// before
modelBuilder.Entity<Order>().HasKey(o => o.Id).HasName("PK_Orders");
modelBuilder.Entity<OrderDetail>().HasKey(o => o.Id).HasName("PK_Details");
// both mapped to table 'Orders'
// after
modelBuilder.Entity<Order>().HasKey(o => o.Id).HasName("PK_Orders");
modelBuilder.Entity<OrderDetail>().HasKey(o => o.Id).HasName("PK_Orders"); Defensive patterns
Strategy: validation
Validate before calling
var byTable = modelBuilder.Model.GetEntityTypes()
.Where(e => !e.IsMappedToJson())
.GroupBy(e => (e.GetTableName(), e.GetSchema()))
.Where(g => g.Count() > 1);
foreach (var grp in byTable)
{
var table = StoreObjectIdentifier.Table(grp.Key.Item1!, grp.Key.Item2);
var names = grp.Select(e => e.FindPrimaryKey()?.GetName(table)).Distinct().ToList();
if (names.Count > 1)
throw new InvalidOperationException(
$"Shared table '{grp.Key.Item1}' has mismatched PK names: {string.Join(", ", names)}");
} Prevention
- Set the PK name (.HasKey(...).HasName(...)) on only one type per shared table, or use the same name everywhere.
- Avoid explicit .HasName() on owned/dependent types — let EF inherit the principal's name.
- Add a CI test that PK names are uniform within each shared-table group.
When it happens
Trigger: Explicitly naming the primary key differently on two types mapped to the same table via `.HasKey(...).HasName("...")`, or having conflicting key name conventions. Detected when traversing directly connected types in ValidateSharedTableCompatibility.
Common situations: Two developers each added a `.HasName()` to their entity's key; merging two contexts that previously had separate tables; explicit key naming for legacy DB alignment where the shared types disagree.
Related errors
- The table '{table}' cannot be used for entity type '{entityT
- The property '{keyProperty}' cannot be configured as 'ValueG
- Entity type '{entityType}' is an optional dependent using ta
- The table '{table}' cannot be used for entity type '{entityT
- The table '{table}' cannot be used for entity type '{entityT
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/373d24936e87abd0.
Report an issue: GitHub.