{"record":{"id":"bb81bde4cc6901cb","repo":"dotnet/efcore","slug":"there-is-no-entity-type-mapped-to-the-table-tabl","errorCode":null,"errorMessage":"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.","messagePattern":"There is no entity type mapped to the 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\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs","lineNumber":1170,"sourceCode":"            }\n\n            yield return modificationCommand;\n        }\n    }\n\n    private static string FormatTable(string table, string? schema)\n        => schema == null ? table : schema + \".\" + table;\n\n    private static IColumnMapping[] GetPropertyMappings(\n        string[] names,\n        string tableName,\n        string? schema,\n        IModel? model)\n    {\n        var table = model?.GetRelationalModel().FindTable(tableName, schema ?? model.GetDefaultSchema());\n        if (table == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DataOperationNoTable(\n                    FormatTable(tableName, schema)));\n        }\n\n        var properties = new IColumnMapping[names.Length];\n        for (var i = 0; i < names.Length; i++)\n        {\n            var name = names[i];\n            var column = table.FindColumn(name);\n            if (column == null)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.DataOperationNoProperty(\n                        FormatTable(tableName, schema), name));\n            }\n\n            properties[i] = column.PropertyMappings.First();\n        }","sourceCodeStart":1152,"sourceCodeEnd":1188,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L1152-L1188","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before (table \"Lookups\" not mapped to any entity, no column types):\nmigrationBuilder.InsertData(\n    table: \"Lookups\",\n    columns: new[] { \"Code\", \"Description\" },\n    values: new object[,] { { \"A\", \"Alpha\" } });\n\n// after (option 1: map the entity so the table exists in the model)\n// in OnModelCreating:\nmodelBuilder.Entity<Lookup>(b =>\n{\n    b.ToTable(\"Lookups\");\n    b.HasKey(x => x.Code);\n});\n// option 2 (model-less): supply columnTypes instead\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 DataOperationTableIsMapped(IModel model, string table, string? schema)\n{\n    var relational = model.GetRelationalModel();\n    return relational.FindTable(table, schema ?? model.GetDefaultSchema()) != null;\n}\n\n// usage: only call InsertData/DeleteData/UpdateData without explicit types if the table is mapped\nif (!DataOperationTableIsMapped(targetModel, \"Lookups\", schema))\n    throw new InvalidOperationException(\"Table is not mapped; supply columnTypes or add the entity type.\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await dbContext.Database.MigrateAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"no entity type mapped to the table\"))\n{\n    logger.LogError(ex, \"Data operation references an unmapped table; add the entity or supply columnTypes.\");\n    throw;\n}","preventionTips":["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."],"tags":["migrations","data-seeding","type-mapping","model","invalidoperation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}