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 is excluded from migrations on one entity type but not on the other. Exclude the table from migrations on all entity types mapped to the table. What it means
Whether a table is excluded from migrations must be consistent for all entity types sharing it, otherwise migrations would try to create/alter a table that another type says should be left alone (e.g. a view-backed or pre-existing table). The validator at line 1251-1258 checks `isExcluded.Equals(!nextEntityType.IsTableExcludedFromMigrations(table))` and throws when one is excluded and the other is not.
Source
Thrown at src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs:1253
&& !comment.Equals(nextComment, StringComparison.Ordinal))
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleTableCommentMismatch(
table.DisplayName(),
entityType.DisplayName(),
nextEntityType.DisplayName(),
comment,
nextComment));
}
}
else
{
comment = nextComment;
}
if (isExcluded.Equals(!nextEntityType.IsTableExcludedFromMigrations(table)))
{
throw new InvalidOperationException(
RelationalStrings.IncompatibleTableExcludedMismatch(
table.DisplayName(),
entityType.DisplayName(),
nextEntityType.DisplayName()));
}
typesToValidate.Enqueue(nextEntityType);
}
foreach (var typeToValidate in typesToValidate.Skip(typesToValidateLeft))
{
unvalidatedTypes.Remove(typeToValidate);
}
}
if (unvalidatedTypes.Count == 0)
{
return;View on GitHub (pinned to dbf9771522)
Solutions
- Call .ExcludeFromMigrations() on every entity type mapped to the shared table.
- Conversely, if migrations should manage the table, remove .ExcludeFromMigrations() from all sharing types.
- Centralize the exclusion in a shared configuration routine applied to all types mapped to the table.
Example fix
// before
modelBuilder.Entity<Order>()
.ToTable("Orders")
.ExcludeFromMigrations();
modelBuilder.Entity<OrderDetail>()
.ToTable("Orders"); // not excluded
// after
modelBuilder.Entity<Order>()
.ToTable("Orders")
.ExcludeFromMigrations();
modelBuilder.Entity<OrderDetail>()
.ToTable("Orders")
.ExcludeFromMigrations(); 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 storeObj = StoreObjectIdentifier.Table(grp.Key.Item1!, grp.Key.Item2);
var excludedFlags = grp.Select(e => e.IsTableExcludedFromMigrations(storeObj)).Distinct().ToList();
if (excludedFlags.Count > 1)
throw new InvalidOperationException(
$"Shared table '{grp.Key.Item1}' has inconsistent ExcludeFromMigrations settings.");
} Prevention
- Apply .ExcludeFromMigrations() uniformly to all types mapped to a shared table (e.g. view-backed or pre-existing tables).
- Wrap the exclusion in a shared configuration extension invoked for every sharing type.
- When converting a table to a view, grep for all .ToTable(...) usages of that name and update each.
When it happens
Trigger: Calling `.ToTable("T").ExcludeFromMigrations()` on one entity but not on another entity mapped to the same table. Detected during the BFS over connected shared-table types.
Common situations: Mapping an entity to a database view or pre-existing table and excluding it from migrations, while an owned/dependent type sharing that table keeps the default (included); refactoring a table to a view and forgetting to update all sharing types.
Related errors
- The table '{table}' cannot be used for entity type '{entityT
- 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
- 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/57f63c448879831e.
Report an issue: GitHub.