dotnet/efcore · error · InvalidOperationException
There is no entity type mapped to the table '{table}' which
Error message
There is no entity type mapped to the table '{table}' which is used in a data operation. Either add the corresponding entity type to the model, or specify the column types in the data operation. What it means
Thrown inside the private GetPropertyMappings(...) helper in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1170 (message key DataOperationNoTable). When a data operation (InsertData/DeleteData/UpdateData) does not supply explicit type arrays, EF looks up the table in the relational model via model.GetRelationalModel().FindTable(tableName, schema). If that returns null (the table is not mapped to any entity type in the model), it throws InvalidOperationException.
Source
Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1170
}
yield return modificationCommand;
}
}
private static string FormatTable(string table, string? schema)
=> schema == null ? table : schema + "." + table;
private static IColumnMapping[] GetPropertyMappings(
string[] names,
string tableName,
string? schema,
IModel? model)
{
var table = model?.GetRelationalModel().FindTable(tableName, schema ?? model.GetDefaultSchema());
if (table == null)
{
throw new InvalidOperationException(
RelationalStrings.DataOperationNoTable(
FormatTable(tableName, schema)));
}
var properties = new IColumnMapping[names.Length];
for (var i = 0; i < names.Length; i++)
{
var name = names[i];
var column = table.FindColumn(name);
if (column == null)
{
throw new InvalidOperationException(
RelationalStrings.DataOperationNoProperty(
FormatTable(tableName, schema), name));
}
properties[i] = column.PropertyMappings.First();
}View on GitHub (pinned to dbf9771522)
Solutions
- Add the corresponding entity type to the model (modelBuilder.Entity<T>().ToTable("<table>", "<schema>")) so FindTable can resolve it, then regenerate the snapshot.
- Supply columnTypes:/keyColumnTypes: on the data operation so EF does not need to look the table up in the model.
- Verify the table name and schema in the migration exactly match the mapped name (case, schema, default-schema) used by the entity.
- If the table was intentionally dropped from the model, remove the data operation (or convert it to a raw Sql(...) call).
- Regenerate the model snapshot (dotnet ef migrations remove then re-add) so the snapshot reflects the entity.
Example fix
// before (table "Lookups" not mapped to any entity, no column types):
migrationBuilder.InsertData(
table: "Lookups",
columns: new[] { "Code", "Description" },
values: new object[,] { { "A", "Alpha" } });
// after (option 1: map the entity so the table exists in the model)
// in OnModelCreating:
modelBuilder.Entity<Lookup>(b =>
{
b.ToTable("Lookups");
b.HasKey(x => x.Code);
});
// option 2 (model-less): supply columnTypes instead
migrationBuilder.InsertData(
table: "Lookups",
columns: new[] { "Code", "Description" },
columnTypes: new[] { "nvarchar(16)", "nvarchar(200)" },
values: new object[,] { { "A", "Alpha" } }); Defensive patterns
Strategy: validation
Validate before calling
static bool DataOperationTableIsMapped(IModel model, string table, string? schema)
{
var relational = model.GetRelationalModel();
return relational.FindTable(table, schema ?? model.GetDefaultSchema()) != null;
}
// usage: only call InsertData/DeleteData/UpdateData without explicit types if the table is mapped
if (!DataOperationTableIsMapped(targetModel, "Lookups", schema))
throw new InvalidOperationException("Table is not mapped; supply columnTypes or add the entity type."); Try / catch
try
{
await dbContext.Database.MigrateAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("no entity type mapped to the table"))
{
logger.LogError(ex, "Data operation references an unmapped table; add the entity or supply columnTypes.");
throw;
} Prevention
- Map every table referenced by data operations to an entity type in the model.
- Keep the table/schema strings in migrations identical to the mapped names (case, schema, default-schema).
- When removing an entity type, also remove or rewrite its data operations.
- For model-less migrations, always supply columnTypes/keyColumnTypes so the table lookup is skipped.
- Regenerate the model snapshot after entity changes so the runtime model matches.
When it happens
Trigger: A migration's InsertData/DeleteData/UpdateData references a table name (and schema) that does not match any entity-mapped table in the IModel, AND no columnTypes/keyColumnTypes are supplied. Common when a lookup/seed table was removed from the model (entity deleted) but its data migration still runs against the model, or when the table name/schema string does not match the mapped name (case sensitivity, schema mismatch, default-schema mismatch).
Common situations: Deleting the entity type but leaving its HasData/InsertData migration in place. Typing a different table name or schema in the migration than what the entity maps to. Mismatched default schema (HasDefaultSchema) between scaffolding time and runtime. Splitting an entity across multiple migrations where the table does not yet exist in the model snapshot.
Related errors
- The data insertion operation on '{table}' is not associated
- The data deletion operation on '{table}' is not associated w
- The data modification operation on '{table}' is not associat
- The number of values ({valuesCount}) doesn't match the numbe
- The number of column types ({typesCount}) doesn't match the
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/bb81bde4cc6901cb.
Report an issue: GitHub.