dotnet/efcore · error · InvalidOperationException

The data insertion operation on '{table}' is not associated

Error message

The data insertion 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(InsertDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:893 (message key InsertDataOperationNoModel). EF needs a CLR/database type mapping for each column to render SQL literals. It can get that either from the model (the entity mapped to the table) or from explicit columnTypes. When both are absent (ColumnTypes == null && model == null) it cannot determine how to format the values and throws InvalidOperationException.

Source

Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:893

            throw new InvalidOperationException(
                RelationalStrings.InsertDataOperationValuesCountMismatch(
                    operation.Values.GetLength(1), operation.Columns.Length,
                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
        }

        if (operation.ColumnTypes != null
            && operation.Columns.Length != operation.ColumnTypes.Length)
        {
            throw new InvalidOperationException(
                RelationalStrings.InsertDataOperationTypesCountMismatch(
                    operation.ColumnTypes.Length, operation.Columns.Length,
                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
        }

        if (operation.ColumnTypes == null
            && model == null)
        {
            throw new InvalidOperationException(
                RelationalStrings.InsertDataOperationNoModel(
                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
        }

        var propertyMappings = operation.ColumnTypes == null
            ? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)
            : null;

        for (var i = 0; i < operation.Values.GetLength(0); i++)
        {
            var modificationCommand = Dependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(
                new NonTrackedModificationCommandParameters(
                    operation.Table, operation.Schema ?? model?.GetDefaultSchema(), SensitiveLoggingEnabled));
            modificationCommand.EntityState = EntityState.Added;

            for (var j = 0; j < operation.Columns.Length; j++)
            {
                var name = operation.Columns[j];

View on GitHub (pinned to dbf9771522)

Solutions

  1. Provide columnTypes: for the InsertData call so EF can build type mappings without a model (every column gets an explicit store type).
  2. Ensure the table is mapped to an entity type in the model (modelBuilder.Entity<T>().ToTable("...")) and that the migration runs with that model attached.
  3. Register the migration assembly with a DesignTimeServiceProvider / MigrationsAssembly that supplies the model.
  4. Move the seed data into modelBuilder.Entity<T>().HasData(...) so it stays associated with the model.

Example fix

// before (no model, no column types):
migrationBuilder.InsertData(
    table: "Lookups",
    columns: new[] { "Code", "Description" },
    values: new object[,] { { "A", "Alpha" } });

// after (supply column types when there is no model):
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 InsertDataHasTypeSource(string[]? columnTypes, IModel? model)
{
    return columnTypes != null || model != null;
}

// usage: if no model is attached, you MUST pass columnTypes
if (!InsertDataHasTypeSource(columnTypes, targetModel))
    throw new InvalidOperationException("InsertData needs a model or explicit columnTypes.");

Try / catch

try
{
    await dbContext.Database.MigrateAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("not associated with a model") && ex.Message.Contains("data insertion"))
{
    logger.LogError(ex, "InsertData on an unmapped table needs explicit columnTypes or a model.");
    throw;
}

Prevention

When it happens

Trigger: Calling migrationBuilder.InsertData on a table that is not mapped to any entity in the model, in a context where no IModel is passed to the migration (model-less migration / MigrationsAssembly built without a model), and without supplying the columnTypes: array. Also when running migrations through a hosting path that does not pass the target model into Migrator.Migrate.

Common situations: Building a standalone migrations DLL with hand-authored migrations that reference tables not in the model. Applying a migration via a path that sets model to null. Seeding a lookup table that was never declared as an entity type. Removing an entity type from the model while leaving its InsertData migration behind.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/ab537592482752b4. Report an issue: GitHub.