{"record":{"id":"ab537592482752b4","repo":"dotnet/efcore","slug":"the-data-insertion-operation-on-table-is-not-a","errorCode":null,"errorMessage":"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.","messagePattern":"The data insertion operation on '(.+?)' is not associated with a model\\. Either add a model to the migration, or specify the column types in all data operations\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs","lineNumber":893,"sourceCode":"            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationValuesCountMismatch(\n                    operation.Values.GetLength(1), operation.Columns.Length,\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        if (operation.ColumnTypes != null\n            && operation.Columns.Length != operation.ColumnTypes.Length)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationTypesCountMismatch(\n                    operation.ColumnTypes.Length, operation.Columns.Length,\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        if (operation.ColumnTypes == null\n            && model == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationNoModel(\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        var propertyMappings = operation.ColumnTypes == null\n            ? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)\n            : null;\n\n        for (var i = 0; i < operation.Values.GetLength(0); i++)\n        {\n            var modificationCommand = Dependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(\n                new NonTrackedModificationCommandParameters(\n                    operation.Table, operation.Schema ?? model?.GetDefaultSchema(), SensitiveLoggingEnabled));\n            modificationCommand.EntityState = EntityState.Added;\n\n            for (var j = 0; j < operation.Columns.Length; j++)\n            {\n                var name = operation.Columns[j];","sourceCodeStart":875,"sourceCodeEnd":911,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L875-L911","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Provide columnTypes: for the InsertData call so EF can build type mappings without a model (every column gets an explicit store type).","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.","Register the migration assembly with a DesignTimeServiceProvider / MigrationsAssembly that supplies the model.","Move the seed data into modelBuilder.Entity<T>().HasData(...) so it stays associated with the model."],"exampleFix":"// before (no model, no column types):\nmigrationBuilder.InsertData(\n    table: \"Lookups\",\n    columns: new[] { \"Code\", \"Description\" },\n    values: new object[,] { { \"A\", \"Alpha\" } });\n\n// after (supply column types when there is no model):\nmigrationBuilder.InsertData(\n    table: \"Lookups\",\n    columns: new[] { \"Code\", \"Description\" },\n    columnTypes: new[] { \"nvarchar(16)\", \"nvarchar(200)\" },\n    values: new object[,] { { \"A\", \"Alpha\" } });","handlingStrategy":"validation","validationCode":"static bool InsertDataHasTypeSource(string[]? columnTypes, IModel? model)\n{\n    return columnTypes != null || model != null;\n}\n\n// usage: if no model is attached, you MUST pass columnTypes\nif (!InsertDataHasTypeSource(columnTypes, targetModel))\n    throw new InvalidOperationException(\"InsertData needs a model or explicit columnTypes.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await dbContext.Database.MigrateAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not associated with a model\") && ex.Message.Contains(\"data insertion\"))\n{\n    logger.LogError(ex, \"InsertData on an unmapped table needs explicit columnTypes or a model.\");\n    throw;\n}","preventionTips":["Map every seeded table to an entity type so the model provides type mappings.","For model-less migrations, always pass columnTypes for every InsertData column.","Run migrations with the runtime model attached (use the standard Migrate path).","Drive seed data through HasData so it stays bound to the model."],"tags":["migrations","data-seeding","insert-data","type-mapping","invalidoperation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}