{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L1201-L1237","documentation":"Thrown when entity types sharing a table have different primary-key names for that table (computed via key.GetName(table)). The validator ensures every sharing type agrees on the PK constraint name; a mismatch would produce conflicting DDL/migration. Names are compared with key.GetName(table) for each connected entity type.","triggerScenarios":"Two entity types mapped to the same table where one has been given a custom PK name via .HasKey(...).HasName(\"PK_Foo\") (or via HasPrincipalKey name on the relationship) and the other uses a different name. Often emerges in TPH/TPT where conventions assign names differently, or when migrations add explicit names.","commonSituations":"Hand-named PK constraints on one entity but not the other; merging two contexts that share a table; explicit constraint names from a migration that did not propagate to siblings.","solutions":["Unify the PK constraint name across all entity types sharing the table: .HasKey(e => e.Id).HasName(\"PK_Users\") on every sharing entity (or remove all explicit HasName so they fall back to the same convention).","If the names truly must differ, the types must not share a table — remap one of them.","Generate/check a migration to confirm the resulting PK constraint name."],"exampleFix":"// before\nbuilder.Entity<Customer>().HasKey(c => c.Id).HasName(\"PK_Customers\");\nbuilder.Entity<Customer>().ToTable(\"Users\");\nbuilder.Entity<User>().ToTable(\"Users\"); // default PK name\n\n// after\nbuilder.Entity<Customer>().HasKey(c => c.Id).HasName(\"PK_Users\");\nbuilder.Entity<User>().HasKey(u => u.Id).HasName(\"PK_Users\");","handlingStrategy":"validation","validationCode":"var byTable = model.GetEntityTypes().Where(e => e.GetTableName() != null).GroupBy(e => e.GetTableName());\nforeach (var grp in byTable)\n{\n    var names = grp.Select(e => (e, e.FindPrimaryKey()?.GetName(StoreObjectIdentifier.Table(grp.Key)))).ToList();\n    var distinct = names.Where(n => n.Item2 != null).Select(n => n.Item2).Distinct().ToList();\n    if (distinct.Count > 1)\n        Console.WriteLine($\"Table '{grp.Key}' has multiple PK names: {string.Join(\", \", distinct)}\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Either never set explicit PK names on shared tables, or set the same HasName on every sharing type.","When migrating an existing database, copy the canonical PK constraint name into the model.","Review PK names whenever you consolidate entity types onto one table."],"tags":["ef-core","table-sharing","keys","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}