{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/RelationalKeyExtensions.cs#L27-L63","documentation":"Thrown by RelationalKeyExtensions.AreCompatible when two keys share the same name AND table but resolve to different column lists. A named key constraint is one physical object, so EF requires the property->column mapping to be identical across all keys sharing the name.","triggerScenarios":"Lines 43-54: both column lists are non-null but columnNames.SequenceEqual(duplicateColumnNames) is false. Produced when HasKey on two table-sharing entity types uses the same name but maps to different properties/columns (e.g. via HasColumnName divergence).","commonSituations":"Table splitting where each entity declares a same-named key over different properties; a property renamed at column level on one entity without updating the key; composite keys where one side added/removed a property.","solutions":["Align the key properties and their HasColumnName on both entity types so the resolved column lists match.","Give each key a distinct name with HasName.","Move the key declaration to a single entity type so there is one source of truth.","Confirm equality with key.Properties.GetColumnNames(storeObject) on both sides."],"exampleFix":"// before\nmodelBuilder.Entity<Account>().HasAlternateKey(a => a.LedgerId).HasName(\"AK_Ledger\");\nmodelBuilder.Entity<Txn>().HasAlternateKey(t => t.JournalId).HasName(\"AK_Ledger\"); // same table, diff cols -> throws\n\n// after\nmodelBuilder.Entity<Txn>().HasAlternateKey(t => t.LedgerId).HasName(\"AK_Ledger\");","handlingStrategy":"validation","validationCode":"static bool KeysColumnMatch(IReadOnlyKey a, IReadOnlyKey b, StoreObjectIdentifier so)\n    => a.GetName(so) == b.GetName(so)\n        && a.Properties.GetColumnNames(so) is { } ca\n        && b.Properties.GetColumnNames(so) is { } cb\n        && ca.SequenceEqual(cb);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep shared keys on the principal entity of a split table.","Use consistent HasColumnName so resolved column lists stay equal."],"tags":["relational","model-validation","key","column-mapping"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}