{"record":{"id":"5ccb88433db5b3c7","repo":"dotnet/efcore","slug":"the-keys-keyproperties1-on-entitytype1-and-5ccb88","errorCode":null,"errorMessage":"The keys {keyProperties1} on '{entityType1}' and {keyProperties2} on '{entityType2}' are both mapped to '{table}.{keyName}', but with different columns ({columnNames1} and {columnNames2}).","messagePattern":"The keys (.+?) on '(.+?)' and (.+?) on '(.+?)' are both mapped to '(.+?)\\.(.+?)', but with different columns \\((.+?) and (.+?)\\)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/RelationalKeyExtensions.cs","lineNumber":45,"sourceCode":"    {\n        var columnNames = key.Properties.GetColumnNames(storeObject);\n        var duplicateColumnNames = duplicateKey.Properties.GetColumnNames(storeObject);\n        return columnNames == null\n            || duplicateColumnNames == null\n                ? shouldThrow\n                    ? throw new InvalidOperationException(\n                        RelationalStrings.DuplicateKeyTableMismatch(\n                            key.Properties.Format(),\n                            key.DeclaringEntityType.DisplayName(),\n                            duplicateKey.Properties.Format(),\n                            duplicateKey.DeclaringEntityType.DisplayName(),\n                            key.GetName(storeObject),\n                            key.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                            duplicateKey.DeclaringEntityType.GetSchemaQualifiedTableName()))\n                    : false\n                : columnNames.SequenceEqual(duplicateColumnNames)\n                || (shouldThrow\n                    ? throw new InvalidOperationException(\n                        RelationalStrings.DuplicateKeyColumnMismatch(\n                            key.Properties.Format(),\n                            key.DeclaringEntityType.DisplayName(),\n                            duplicateKey.Properties.Format(),\n                            duplicateKey.DeclaringEntityType.DisplayName(),\n                            key.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                            key.GetName(storeObject),\n                            key.Properties.FormatColumns(storeObject),\n                            duplicateKey.Properties.FormatColumns(storeObject)))\n                    : false);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/RelationalKeyExtensions.cs#L27-L63","documentation":"Two keys share the same database name and table but map to different columns. A single key constraint name cannot identify two different column sets.","triggerScenarios":"RelationalKeyExtensions.AreCompatible with shouldThrow:true when !columnNames.SequenceEqual(duplicateColumnNames). Triggered when two entities on the same table each define a key whose properties resolve to different columns under a shared constraint name.","commonSituations":"Table-splitting where the owner defines a PK and a partner entity defines an alternate key with a colliding name; explicit HasName copy/paste errors across different properties.","solutions":["Give each key a distinct name via HasName(...).","Map both keys to the same columns if they are meant to be the same constraint.","Remove the redundant key.","Audit HasName usages across entities that share a table."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasKey(o => o.Id).HasName(\"PK_Order\");\nmodelBuilder.Entity<OrderSnapshot>().ToTable(\"Order\").HasKey(s => s.SnapshotId).HasName(\"PK_Order\");\n\n// after\nmodelBuilder.Entity<OrderSnapshot>().ToTable(\"Order\").HasKey(s => s.SnapshotId).HasName(\"PK_Order_Snapshot\");","handlingStrategy":"validation","validationCode":"// Detect key-name collisions with differing columns on the same table.\nvar groups = modelBuilder.Model.GetEntityTypes()\n    .SelectMany(e => e.GetDeclaredKeys())\n    .GroupBy(k => new { Name = k.GetName(), Table = k.DeclaringEntityType.GetSchemaQualifiedTableName() })\n    .Where(g => g.Count() > 1);\nforeach (var g in groups)\n{\n    var cols = g.Select(k => string.Join(\",\", k.Properties.Select(p => p.GetColumnName()))).Distinct();\n    if (cols.Count() > 1)\n        Console.WriteLine($\"Key '{g.Key.Name}' on '{g.Key.Table}' has conflicting columns.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Give keys distinct HasName when entities share a table but define different key shapes.","Review key definitions during table-splitting setup.","Validate the model in tests."],"tags":["ef-core","relational-metadata","key","model-validation","table-splitting"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}