{"record":{"id":"760924b796bce066","repo":"dotnet/efcore","slug":"the-table-table-cannot-be-used-for-entity-type-760924","errorCode":null,"errorMessage":"The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and potentially other entity types, but there is no linking relationship. Add a foreign key to '{entityType}' on the primary key properties and pointing to the primary key on another entity type mapped to '{table}'.","messagePattern":"The table '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and potentially other entity types, but there is no linking relationship\\. Add a foreign key to '(.+?)' on the primary key properties and pointing to the primary key on another entity type mapped to '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1186,"sourceCode":"                    .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\n            root = mappedType;\n        }\n\n        Check.DebugAssert(root != null);\n        unvalidatedTypes.Remove(root);\n        var typesToValidate = new Queue<IEntityType>();\n        typesToValidate.Enqueue(root);\n\n        while (typesToValidate.Count > 0)\n        {\n            var entityType = typesToValidate.Dequeue();\n            var key = entityType.FindPrimaryKey();","sourceCodeStart":1168,"sourceCodeEnd":1204,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1168-L1204","documentation":"When two or more entity types are mapped to the same table, EF requires them to be connected either by inheritance or by an identifying foreign key (a FK on the dependent's primary key pointing at the principal's PK). The validator at line 1184-1191 throws when it encounters a second 'root' (an entity type with no connecting relationship to the first root) sharing the table — EF cannot decide which type owns the row.","triggerScenarios":"Calling `.ToTable(\"SameTable\")` on two unrelated entity types without defining a foreign key between their primary keys. Detected at the initial root-finding loop (line 1156-1194) when `root` is already set and a new unconnected type is found.","commonSituations":"Manually co-locating two entities in one table for performance without setting up the relationship; renaming a table to collide with another entity's table; CUD stored procedure or view scenarios where sharing was intended but the FK was forgotten.","solutions":["Add a foreign key on the dependent entity's primary key properties pointing to the principal's primary key: .HasOne(...).HasForeignKey(...).","If the two types are meant to be one row, model one as owned by the other (.OwnsOne) so EF creates the implicit FK.","If they are genuinely unrelated, map them to different tables."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().ToTable(\"Records\");\nmodelBuilder.Entity<OrderAudit>().ToTable(\"Records\"); // no FK\n\n// after\nmodelBuilder.Entity<OrderAudit>()\n    .HasOne<Order>()\n    .WithOne()\n    .HasForeignKey<OrderAudit>(a => a.Id);\nmodelBuilder.Entity<Order>().ToTable(\"Records\");\nmodelBuilder.Entity<OrderAudit>().ToTable(\"Records\");","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 types = grp.ToList();\n    for (int i = 1; i < types.Count; i++)\n    {\n        bool connected = types[i].BaseType != null && types.Contains(types[i].BaseType)\n            || HasLinkingFk(types[i], types[0])\n            || HasLinkingFk(types[0], types[i]);\n        if (!connected)\n            throw new InvalidOperationException(\n                $\"Types {types[0].Name} and {types[i].Name} share table without a linking FK.\");\n    }\n}\nbool HasLinkingFk(IEntityType dep, IEntityType prin)\n    => dep.FindPrimaryKey() is { } pk\n       && dep.FindForeignKeys(pk.Properties).Any(fk =>\n            fk.PrincipalKey.IsPrimaryKey()\n            && fk.PrincipalEntityType == prin\n            && !fk.PrincipalEntityType.IsAssignableFrom(dep));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whenever two entity types share a table, define a one-to-one FK on their primary keys.","Use .OwnsOne for true table splitting so EF creates the relationship implicitly.","Add a test that asserts every shared-table group is a single connected component."],"tags":["ef-core","table-sharing","model-validation","relationships"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}