{"record":{"id":"c0058855d5bd3240","repo":"dotnet/efcore","slug":"the-table-table-cannot-be-used-for-entity-type","errorCode":null,"errorMessage":"The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and there is a relationship between their primary keys in which '{entityType}' is the dependent, but '{entityType}' has a base entity type mapped to a different table. Either map '{otherEntityType}' to a different table, or invert the relationship between '{entityType}' and '{otherEntityType}'.","messagePattern":"The table '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and there is a relationship between their primary keys in which '(.+?)' is the dependent, but '(.+?)' has a base entity type mapped to a different table\\. Either map '(.+?)' to a different table, or invert the relationship between '(.+?)' and '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1174,"sourceCode":"        var unvalidatedTypes = new HashSet<IEntityType>(mappedTypes);\n        IEntityType? root = null;\n        foreach (var mappedType in mappedTypes)\n        {\n            if (mappedType.BaseType != null && unvalidatedTypes.Contains(mappedType.BaseType))\n            {\n                continue;\n            }\n\n            var primaryKey = mappedType.FindPrimaryKey();\n            if (primaryKey != null\n                && (mappedType.FindForeignKeys(primaryKey.Properties)\n                    .FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()\n                        && !fk.PrincipalEntityType.IsAssignableFrom(fk.DeclaringEntityType)\n                        && unvalidatedTypes.Contains(fk.PrincipalEntityType)) is { } linkingFK))\n            {\n                if (mappedType.BaseType != null)\n                {\n                    throw new InvalidOperationException(\n                        RelationalStrings.IncompatibleTableDerivedRelationship(\n                            table.DisplayName(),\n                            mappedType.DisplayName(),\n                            linkingFK.PrincipalEntityType.DisplayName()));\n                }\n\n                continue;\n            }\n\n            if (root != null)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.IncompatibleTableNoRelationship(\n                        table.DisplayName(),\n                        mappedType.DisplayName(),\n                        root.DisplayName()));\n            }\n","sourceCodeStart":1156,"sourceCodeEnd":1192,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1156-L1192","documentation":"In shared-table mapping, a dependent entity is linked to its principal via a foreign key on the primary key. If that dependent also has a base entity type (inheritance) that is mapped to a different table, the sharing is inconsistent — the dependent's row cannot live in both tables. The validator at line 1172-1179 detects this when it finds a linking FK to another shared type but the current type already has a base type mapped elsewhere.","triggerScenarios":"Combining inheritance (TPH/TPC/TPT) with table splitting such that a derived/owned entity has both a base type on a different table and an identifying FK to a principal on the shared table. Triggered during ValidateSharedTableCompatibility traversal at line 1158-1194.","commonSituations":"Mixing TPT (table-per-type) with owned entity table sharing; refactoring an inheritance hierarchy so a type that used to share now has a base elsewhere; misconfigured ToTable on an owned derived type.","solutions":["Map the principal entity (otherEntityType) to a different table so the dependent's inheritance chain and sharing do not conflict.","Invert the relationship so the entity currently marked dependent becomes the principal, removing the FK-on-PK dependency.","Unify the table mapping: ensure the dependent's base type is also mapped to the same table (or break the inheritance link)."],"exampleFix":"// before: Detail derives from AuditEntity (mapped to 'audits')\n//         but shares table 'Orders' with Order via PK FK\nmodelBuilder.Entity<Order>().OwnsOne(o => o.Detail);\nmodelBuilder.Entity<AuditEntity>().ToTable(\"audits\");\n\n// after: map Order to a distinct table so no conflict\nmodelBuilder.Entity<Order>().ToTable(\"Orders\");\nmodelBuilder.Entity<Order>().OwnsOne(o => o.Detail);\n// and ensure Detail's base is not also mapped to 'Orders'","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    if (et.BaseType == null) continue;\n    var table = et.GetTableName();\n    if (table == null) continue;\n    var baseTable = et.BaseType.GetTableName();\n    if (table == baseTable) continue; // consistent\n\n    var pk = et.FindPrimaryKey();\n    if (pk == null) continue;\n    var linkingFk = et.FindForeignKeys(pk.Properties)\n        .FirstOrDefault(fk => fk.PrincipalKey.IsPrimaryKey()\n            && !fk.PrincipalEntityType.IsAssignableFrom(et)\n            && fk.PrincipalEntityType.GetTableName() == table);\n    if (linkingFk != null)\n        throw new InvalidOperationException(\n            $\"{et.Name} maps table '{table}' with a linking FK but its base maps '{baseTable}'.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep inheritance and table-sharing orthogonal: a derived type that shares a table should have its base on the same table.","Avoid TPT-style mappings combined with owned-type table splitting.","Review ToTable calls on every type in an inheritance hierarchy after refactoring."],"tags":["ef-core","table-sharing","inheritance","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}