{"record":{"id":"373d24936e87abd0","repo":"dotnet/efcore","slug":"the-table-table-cannot-be-used-for-entity-type-373d24","errorCode":null,"errorMessage":"The table '{table}' cannot be used for entity type '{entityType}' since it is being used for entity type '{otherEntityType}' and the name '{keyName}' of the primary key {primaryKey} does not match the name '{otherName}' of the primary key {otherPrimaryKey}.","messagePattern":"The table '(.+?)' cannot be used for entity type '(.+?)' since it is being used for entity type '(.+?)' and the name '(.+?)' of the primary key (.+?) does not match the name '(.+?)' of the primary key (.+?)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1219,"sourceCode":"        while (typesToValidate.Count > 0)\n        {\n            var entityType = typesToValidate.Dequeue();\n            var key = entityType.FindPrimaryKey();\n            var comment = entityType.GetComment();\n            var isExcluded = entityType.IsTableExcludedFromMigrations(table);\n            var typesToValidateLeft = typesToValidate.Count;\n            var directlyConnectedTypes = unvalidatedTypes.Where(unvalidatedType =>\n                entityType.IsAssignableFrom(unvalidatedType)\n                || IsIdentifyingPrincipal(unvalidatedType, entityType));\n\n            foreach (var nextEntityType in directlyConnectedTypes)\n            {\n                if (key != null)\n                {\n                    var otherKey = nextEntityType.FindPrimaryKey()!;\n                    if (key.GetName(table) != otherKey.GetName(table))\n                    {\n                        throw new InvalidOperationException(\n                            RelationalStrings.IncompatibleTableKeyNameMismatch(\n                                table.DisplayName(),\n                                entityType.DisplayName(),\n                                nextEntityType.DisplayName(),\n                                key.GetName(table),\n                                key.Properties.Format(),\n                                otherKey.GetName(table),\n                                otherKey.Properties.Format()));\n                    }\n                }\n\n                var nextComment = nextEntityType.GetComment();\n                if (comment != null)\n                {\n                    if (nextComment != null\n                        && !comment.Equals(nextComment, StringComparison.Ordinal))\n                    {\n                        throw new InvalidOperationException(","sourceCodeStart":1201,"sourceCodeEnd":1237,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1201-L1237","documentation":"All entity types sharing a table must agree on the primary key constraint name so that migrations emit a single, consistent PK. The validator at line 1217-1228 compares `key.GetName(table)` between connected types during the BFS traversal (line 1212-1228) and throws when the names differ, reporting both names and property lists.","triggerScenarios":"Explicitly naming the primary key differently on two types mapped to the same table via `.HasKey(...).HasName(\"...\")`, or having conflicting key name conventions. Detected when traversing directly connected types in ValidateSharedTableCompatibility.","commonSituations":"Two developers each added a `.HasName()` to their entity's key; merging two contexts that previously had separate tables; explicit key naming for legacy DB alignment where the shared types disagree.","solutions":["Make the key name identical on all entity types sharing the table: call .HasKey(...).HasName(\"<sameName>\") on each, or remove explicit names so the default is computed consistently.","Verify there are not two different .HasName() calls producing the mismatch shown in the error.","If the names must differ for legacy reasons, map the types to different tables."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasKey(o => o.Id).HasName(\"PK_Orders\");\nmodelBuilder.Entity<OrderDetail>().HasKey(o => o.Id).HasName(\"PK_Details\");\n// both mapped to table 'Orders'\n\n// after\nmodelBuilder.Entity<Order>().HasKey(o => o.Id).HasName(\"PK_Orders\");\nmodelBuilder.Entity<OrderDetail>().HasKey(o => o.Id).HasName(\"PK_Orders\");","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 table = StoreObjectIdentifier.Table(grp.Key.Item1!, grp.Key.Item2);\n    var names = grp.Select(e => e.FindPrimaryKey()?.GetName(table)).Distinct().ToList();\n    if (names.Count > 1)\n        throw new InvalidOperationException(\n            $\"Shared table '{grp.Key.Item1}' has mismatched PK names: {string.Join(\", \", names)}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set the PK name (.HasKey(...).HasName(...)) on only one type per shared table, or use the same name everywhere.","Avoid explicit .HasName() on owned/dependent types — let EF inherit the principal's name.","Add a CI test that PK names are uniform within each shared-table group."],"tags":["ef-core","table-sharing","model-validation","keys","migrations"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}