dotnet/efcore · error · InvalidOperationException
The data deletion operation on '{table}' is not associated w
Error message
The data deletion 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(DeleteDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:985 (message key DeleteDataOperationNoModel). EF needs a type mapping for each key column to build the WHERE clause. It gets this from the model (entity mapped to the table) or from explicit KeyColumnTypes. When both are absent (KeyColumnTypes == null && model == null) it throws InvalidOperationException.
Source
Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:985
if (operation.KeyColumns.Length != operation.KeyValues.GetLength(1))
{
throw new InvalidOperationException(
RelationalStrings.DeleteDataOperationValuesCountMismatch(
operation.KeyValues.GetLength(1), operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));
}
if (operation.KeyColumnTypes != null
&& operation.KeyColumns.Length != operation.KeyColumnTypes.Length)
{
throw new InvalidOperationException(
RelationalStrings.DeleteDataOperationTypesCountMismatch(
operation.KeyColumnTypes.Length, operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));
}
if (operation.KeyColumnTypes == null
&& model == null)
{
throw new InvalidOperationException(
RelationalStrings.DeleteDataOperationNoModel(
FormatTable(operation.Table, operation.Schema)));
}
var keyPropertyMappings = operation.KeyColumnTypes == null
? GetPropertyMappings(operation.KeyColumns, 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.Deleted;
for (var j = 0; j < operation.KeyColumns.Length; j++)
{
var name = operation.KeyColumns[j];
var value = operation.KeyValues[i, j];View on GitHub (pinned to dbf9771522)
Solutions
- Supply keyColumnTypes: on the DeleteData call so EF can build key 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.
- Use HasData and let EF scaffold the corresponding DeleteData with the model.
- Register the migrations assembly with a provider that supplies the runtime model.
Example fix
// before (no model, no key types):
migrationBuilder.DeleteData(
table: "Lookups",
keyColumns: new[] { "Code" },
keyValues: new object[,] { { "A" } });
// after (supply key types when there is no model):
migrationBuilder.DeleteData(
table: "Lookups",
keyColumns: new[] { "Code" },
keyColumnTypes: new[] { "nvarchar(16)" },
keyValues: new object[,] { { "A" } }); Defensive patterns
Strategy: validation
Validate before calling
static bool DeleteDataHasTypeSource(string[]? keyColumnTypes, IModel? model)
{
return keyColumnTypes != null || model != null;
}
// usage: if no model is attached, you MUST pass keyColumnTypes
if (!DeleteDataHasTypeSource(keyColumnTypes, targetModel))
throw new InvalidOperationException("DeleteData 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 deletion"))
{
logger.LogError(ex, "DeleteData on an unmapped table needs explicit keyColumnTypes 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 for every DeleteData key column.
- 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.DeleteData 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 into the generator while the migration contains DeleteData.
Common situations: Standalone migrations assemblies with hand-authored DeleteData for unmapped tables. Removing an entity type from the model while its DeleteData migration remains. Running migrations via a custom host that does not supply the model.
Related errors
- The data insertion operation on '{table}' is not associated
- The number of key values ({valuesCount}) doesn't match the n
- The number of key column types ({typesCount}) doesn't match
- The data modification operation on '{table}' is not associat
- There is no entity type mapped to the table '{table}' which
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/cb1878f5861b7a6a.
Report an issue: GitHub.