{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1156-L1192","documentation":"Thrown by ValidateSharedTableCompatibility when multiple entity types share a table, one of them (mappedType) has a foreign key on its primary key pointing to another sharing type (a linking FK), and mappedType has a BaseType — i.e. it is in an inheritance hierarchy mapped to a different table. Joining a derived entity to a sibling via PK-FK while splitting its hierarchy across tables is contradictory and rejected.","triggerScenarios":"Entity types A and B share a table; B's primary key has an FK to A's primary key; B also derives from a base entity mapped to another table. The validator detects mappedType.BaseType != null alongside the linking FK and throws.","commonSituations":"TPT or TPC inheritance combined with table sharing / many-to-many link tables; refactoring inheritance while leaving an old FK-based table-share in place.","solutions":["Map one of the two types to its own table so the derived relationship is no longer split: builder.ToTable(\"B\").","Invert the relationship so the dependent becomes the principal of the linking FK and the base-type constraint is satisfied.","Resolve the inheritance conflict: either flatten the hierarchy for these types or remap the base type to the same table."],"exampleFix":"// before\n// class Derived : Base, where Base -> \"BaseTable\", Derived shares \"SharedTable\" with Principal\n[Table(\"SharedTable\")] public class Derived : Base { ... }\nbuilder.Entity<Derived>().HasOne(d => d.Principal).WithOne().HasForeignKey<Derived>(d => d.Id);\n\n// after\nbuilder.Entity<Derived>().ToTable(\"DerivedTable\"); // break the share","handlingStrategy":"validation","validationCode":"foreach (var et in model.GetEntityTypes())\n{\n    if (et.BaseType == null) continue;\n    var pk = et.FindPrimaryKey();\n    if (pk == null) continue;\n    foreach (var fk in et.FindForeignKeys(pk.Properties))\n    {\n        if (fk.PrincipalKey.IsPrimaryKey()\n            && !fk.PrincipalEntityType.IsAssignableFrom(et)\n            && !string.Equals(et.GetTableName(), fk.PrincipalEntityType.GetTableName(), StringComparison.Ordinal))\n        {\n            Console.WriteLine($\"{et.Name} derives from {et.BaseType.Name} (different table) but has PK FK to {fk.PrincipalEntityType.Name} on the same table.\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep inheritance hierarchies on a single shared table when linking PKs across types.","When splitting types across tables, avoid PK-to-PK FKs between siblings.","Run model validation in tests whenever you change ToTable mapping for an inheritance chain."],"tags":["ef-core","table-sharing","inheritance","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}