dotnet/efcore · error · InvalidOperationException
The data modification operation on '{table}' is not associat
Error message
The data modification operation on '{table}' is not associated with a model. Either add a model to the migration, or specify the column types in all data operations. What it means
Thrown inside GenerateModificationCommands(UpdateDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1098 (message key UpdateDataOperationNoModel). EF needs a type mapping for both the key columns (WHERE clause) and the updated columns (SET clause). It gets these from the model or from explicit type arrays; the code specifically checks the KEY side: when KeyColumnTypes == null && model == null it throws InvalidOperationException (the SET side is then also unreachable).
Source
Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1098
&& operation.KeyColumns.Length != operation.KeyColumnTypes.Length)
{
throw new InvalidOperationException(
RelationalStrings.UpdateDataOperationKeyTypesCountMismatch(
operation.KeyColumnTypes.Length, operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));
}
if (operation.ColumnTypes != null
&& operation.Columns.Length != operation.ColumnTypes.Length)
{
throw new InvalidOperationException(
RelationalStrings.UpdateDataOperationTypesCountMismatch(
operation.ColumnTypes.Length, operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));
}
if (operation.KeyColumnTypes == null
&& model == null)
{
throw new InvalidOperationException(
RelationalStrings.UpdateDataOperationNoModel(
FormatTable(operation.Table, operation.Schema)));
}
var keyPropertyMappings = operation.KeyColumnTypes == null
? GetPropertyMappings(operation.KeyColumns, operation.Table, operation.Schema, model)
: null;
var propertyMappings = operation.ColumnTypes == null
? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)
: null;
for (var i = 0; i < operation.KeyValues.GetLength(0); i++)
{
var modificationCommand = Dependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(
new NonTrackedModificationCommandParameters(operation.Table, operation.Schema, SensitiveLoggingEnabled));
modificationCommand.EntityState = EntityState.Modified;
for (var j = 0; j < operation.KeyColumns.Length; j++)View on GitHub (pinned to dbf9771522)
Solutions
- Supply keyColumnTypes: (and columnTypes:) on the UpdateData call so EF can build type mappings without a model.
- Map the table to an entity type (modelBuilder.Entity<T>().ToTable("...")) and ensure the migration runs with that model attached.
- Manage seed data through HasData so EF scaffolds UpdateData with the model.
- Register the migrations assembly with a provider that supplies the runtime model.
Example fix
// before (no model, no key types):
migrationBuilder.UpdateData(
table: "Lookups",
keyColumns: new[] { "Code" },
keyValues: new object[,] { { "A" } },
columns: new[] { "Description" },
values: new object[,] { { "Alpha" } });
// after (supply key types when there is no model):
migrationBuilder.UpdateData(
table: "Lookups",
keyColumns: new[] { "Code" },
keyColumnTypes: new[] { "nvarchar(16)" },
keyValues: new object[,] { { "A" } },
columns: new[] { "Description" },
columnTypes: new[] { "nvarchar(200)" },
values: new object[,] { { "Alpha" } }); Defensive patterns
Strategy: validation
Validate before calling
static bool UpdateDataHasTypeSource(string[]? keyColumnTypes, IModel? model)
{
return keyColumnTypes != null || model != null;
}
// usage: if no model is attached, you MUST pass keyColumnTypes (columnTypes recommended too)
if (!UpdateDataHasTypeSource(keyColumnTypes, targetModel))
throw new InvalidOperationException("UpdateData needs a model or explicit keyColumnTypes."); Try / catch
try
{
await dbContext.Database.MigrateAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("not associated with a model") && ex.Message.Contains("data modification"))
{
logger.LogError(ex, "UpdateData on an unmapped table needs explicit keyColumnTypes (and columnTypes) or a model.");
throw;
} Prevention
- Map every seeded table to an entity type so the model provides key type mappings.
- For model-less migrations, always pass keyColumnTypes (and columnTypes) for UpdateData.
- Run migrations with the runtime model attached.
- Drive seed data through HasData so it stays bound to the model.
When it happens
Trigger: Calling migrationBuilder.UpdateData on a table not mapped to any entity, in a model-less migration, without supplying keyColumnTypes:. Also when migrations are applied through a code path that passes model == null while the migration contains UpdateData. Note: you must supply keyColumnTypes at minimum; supplying only columnTypes is not enough to satisfy this guard.
Common situations: Standalone migrations assemblies with hand-authored UpdateData for unmapped tables. Removing an entity type from the model while leaving its UpdateData migration behind. Running migrations via a custom host that does not supply the model.
Related errors
- The data insertion operation on '{table}' is not associated
- The data deletion operation on '{table}' is not associated w
- The number of key values ({valuesCount}) doesn't match the n
- The number of values ({valuesCount}) doesn't match the numbe
- The number of value rows ({valuesCount}) doesn't match the n
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/dc4b160bf1ed2af9.
Report an issue: GitHub.