{"record":{"id":"57f63c448879831e","repo":"dotnet/efcore","slug":"the-table-table-cannot-be-used-for-entity-type-57f63c","errorCode":null,"errorMessage":"The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and is excluded from migrations on one entity type but not on the other. Exclude the table from migrations on all entity types mapped to the table.","messagePattern":"The table '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and is excluded from migrations on one entity type but not on the other\\. Exclude the table from migrations on all entity types mapped to the table\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1253,"sourceCode":"                        && !comment.Equals(nextComment, StringComparison.Ordinal))\n                    {\n                        throw new InvalidOperationException(\n                            RelationalStrings.IncompatibleTableCommentMismatch(\n                                table.DisplayName(),\n                                entityType.DisplayName(),\n                                nextEntityType.DisplayName(),\n                                comment,\n                                nextComment));\n                    }\n                }\n                else\n                {\n                    comment = nextComment;\n                }\n\n                if (isExcluded.Equals(!nextEntityType.IsTableExcludedFromMigrations(table)))\n                {\n                    throw new InvalidOperationException(\n                        RelationalStrings.IncompatibleTableExcludedMismatch(\n                            table.DisplayName(),\n                            entityType.DisplayName(),\n                            nextEntityType.DisplayName()));\n                }\n\n                typesToValidate.Enqueue(nextEntityType);\n            }\n\n            foreach (var typeToValidate in typesToValidate.Skip(typesToValidateLeft))\n            {\n                unvalidatedTypes.Remove(typeToValidate);\n            }\n        }\n\n        if (unvalidatedTypes.Count == 0)\n        {\n            return;","sourceCodeStart":1235,"sourceCodeEnd":1271,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1235-L1271","documentation":"Whether a table is excluded from migrations must be consistent for all entity types sharing it, otherwise migrations would try to create/alter a table that another type says should be left alone (e.g. a view-backed or pre-existing table). The validator at line 1251-1258 checks `isExcluded.Equals(!nextEntityType.IsTableExcludedFromMigrations(table))` and throws when one is excluded and the other is not.","triggerScenarios":"Calling `.ToTable(\"T\").ExcludeFromMigrations()` on one entity but not on another entity mapped to the same table. Detected during the BFS over connected shared-table types.","commonSituations":"Mapping an entity to a database view or pre-existing table and excluding it from migrations, while an owned/dependent type sharing that table keeps the default (included); refactoring a table to a view and forgetting to update all sharing types.","solutions":["Call .ExcludeFromMigrations() on every entity type mapped to the shared table.","Conversely, if migrations should manage the table, remove .ExcludeFromMigrations() from all sharing types.","Centralize the exclusion in a shared configuration routine applied to all types mapped to the table."],"exampleFix":"// before\nmodelBuilder.Entity<Order>()\n    .ToTable(\"Orders\")\n    .ExcludeFromMigrations();\nmodelBuilder.Entity<OrderDetail>()\n    .ToTable(\"Orders\"); // not excluded\n\n// after\nmodelBuilder.Entity<Order>()\n    .ToTable(\"Orders\")\n    .ExcludeFromMigrations();\nmodelBuilder.Entity<OrderDetail>()\n    .ToTable(\"Orders\")\n    .ExcludeFromMigrations();","handlingStrategy":"validation","validationCode":"var byTable = modelBuilder.Model.GetEntityTypes()\n    .Where(e => !e.IsMappedToJson())\n    .GroupBy(e => (e.GetTableName(), e.GetSchema()))\n    .Where(g => g.Count() > 1);\nforeach (var grp in byTable)\n{\n    var storeObj = StoreObjectIdentifier.Table(grp.Key.Item1!, grp.Key.Item2);\n    var excludedFlags = grp.Select(e => e.IsTableExcludedFromMigrations(storeObj)).Distinct().ToList();\n    if (excludedFlags.Count > 1)\n        throw new InvalidOperationException(\n            $\"Shared table '{grp.Key.Item1}' has inconsistent ExcludeFromMigrations settings.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply .ExcludeFromMigrations() uniformly to all types mapped to a shared table (e.g. view-backed or pre-existing tables).","Wrap the exclusion in a shared configuration extension invoked for every sharing type.","When converting a table to a view, grep for all .ToTable(...) usages of that name and update each."],"tags":["ef-core","table-sharing","migrations","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}