{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1235-L1271","documentation":"Thrown when entity types sharing a table disagree on whether the table is excluded from migrations (IsTableExcludedFromMigrations). The validator stores isExcluded for the first type and compares !nextEntityType.IsTableExcludedFromMigrations(table) with that flag; if one entity opts out of migrations for the shared table and another does not, the configuration is contradictory and the migration generator would emit conflicting behavior.","triggerScenarios":"builder.Entity<A>().ToTable(\"X\").ExcludeFromMigrations() while builder.Entity<B>().ToTable(\"X\") does not exclude. Common when an owned entity is excluded but its owner is not, or when one half of a 1:1 table share is excluded by mistake.","commonSituations":"Marking only the principal or only the dependent as excluded; multi-tenant pre-provisioned tables where one entity type is excluded; refactoring migrations exclusion logic incompletely.","solutions":["Apply the same exclusion choice to every entity type sharing the table: all excluded, or none excluded.","Use builder.Entity<A>().ToTable(\"X\", t => t.ExcludeFromMigrations()) and replicate on every sharing entity, or remove the call everywhere.","For owned entities, exclude from the owner so they cascade — verify with ToView() vs ToTable() choices if the table should be read-only."],"exampleFix":"// before\nbuilder.Entity<Customer>().ToTable(\"Users\").ExcludeFromMigrations();\nbuilder.Entity<User>().ToTable(\"Users\");\n\n// after\nbuilder.Entity<Customer>().ToTable(\"Users\", t => t.ExcludeFromMigrations());\nbuilder.Entity<User>().ToTable(\"Users\", t => t.ExcludeFromMigrations());","handlingStrategy":"validation","validationCode":"var byTable = model.GetEntityTypes().Where(e => e.GetTableName() != null).GroupBy(e => e.GetTableName());\nforeach (var grp in byTable)\n{\n    var flags = grp.Select(e => e.IsTableExcludedFromMigrations(StoreObjectIdentifier.Table(grp.Key))).Distinct().ToList();\n    if (flags.Count > 1)\n        Console.WriteLine($\"Table '{grp.Key}' has inconsistent ExcludeFromMigrations across entity types.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply ExcludeFromMigrations uniformly to all entity types sharing a table.","For owned entities, set exclusion on the owner so it cascades.","Document which tables are pre-provisioned and excluded from migrations."],"tags":["ef-core","table-sharing","migrations","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}