{"record":{"id":"ae3e4d12d31097d3","repo":"dotnet/efcore","slug":"the-foreign-keys-foreignkeyproperties1-on-enti-ae3e4d","errorCode":null,"errorMessage":"The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but use different columns ({columnNames1} and {columnNames2}).","messagePattern":"The foreign keys (.+?) on '(.+?)' and (.+?) on '(.+?)' are both mapped to '(.+?)\\.(.+?)', but use different columns \\((.+?) and (.+?)\\)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs","lineNumber":85,"sourceCode":"                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyPrincipalTableMismatch(\n                        foreignKey.Properties.Format(),\n                        foreignKey.DeclaringEntityType.DisplayName(),\n                        duplicateForeignKey.Properties.Format(),\n                        duplicateForeignKey.DeclaringEntityType.DisplayName(),\n                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                        principalTable.HasValue\n                            ? foreignKey.GetConstraintName(storeObject, principalTable.Value)\n                            : foreignKey.GetDefaultName(),\n                        principalType.GetSchemaQualifiedTableName(),\n                        duplicatePrincipalType.GetSchemaQualifiedTableName()))\n                : false;\n        }\n\n        if (!columnNames.SequenceEqual(duplicateColumnNames))\n        {\n            return shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyColumnMismatch(\n                        foreignKey.Properties.Format(),\n                        foreignKey.DeclaringEntityType.DisplayName(),\n                        duplicateForeignKey.Properties.Format(),\n                        duplicateForeignKey.DeclaringEntityType.DisplayName(),\n                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                        foreignKey.GetConstraintName(storeObject, principalTable.Value),\n                        foreignKey.Properties.FormatColumns(storeObject),\n                        duplicateForeignKey.Properties.FormatColumns(storeObject)))\n                : false;\n        }\n\n        if (!principalColumns.SequenceEqual(duplicatePrincipalColumns))\n        {\n            return shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyPrincipalColumnMismatch(\n                        foreignKey.Properties.Format(),","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L67-L103","documentation":"Thrown by AreCompatible when two foreign keys share name, dependent table, and principal table, but the dependent-side column names differ. One database FK constraint must reference a fixed set of dependent columns, so divergent column sets cannot be unified.","triggerScenarios":"Two sharing-table FKs with the same constraint name map their FK properties to different column names on the shared table. Happens when properties are renamed via HasColumnName on one but not the other, or when two FKs reuse a name but use different property sets.","commonSituations":"TPH hierarchy where base and derived define FKs resolving to one name, but the column mappings diverge. Column renaming in migrations without updating FK config.","solutions":["Map the colliding FK properties to the same column names (align HasColumnName).","Give the FKs distinct constraint names so each owns its column set.","Ensure the FK properties themselves are the same set when they are meant to be one constraint.","Audit the dependent column names of each colliding FK with GetColumnNames(storeObject)."],"exampleFix":"// before (two FKs same name, dependent columns OrderId vs OrderRef)\nmodelBuilder.Entity<Allocation>().HasOne(a => a.Order)\n    .WithMany().HasForeignKey(a => a.OrderId).HasConstraintName(\"FK_Alloc_Ref\");\nmodelBuilder.Entity<Allocation>().HasOne(a => a.Order)\n    .WithMany().HasForeignKey(a => a.OrderRef).HasConstraintName(\"FK_Alloc_Ref\")\n    .OnDelete(DeleteBehavior.Restrict);\n\n// after (unify columns or rename one constraint)\nmodelBuilder.Entity<Allocation>().Property(a => a.OrderRef).HasColumnName(\"OrderId\");\n// or give the second FK a distinct name","handlingStrategy":"validation","validationCode":"// Verify FKs sharing a name map to identical dependent column names\nforeach (var g in allFks.GroupBy(fk => fk.GetConstraintName()))\n{\n    var colSets = g.Select(fk => string.Join(\",\", fk.Properties.GetColumnNames(store))).Distinct();\n    if (colSets.Count() > 1)\n        throw new InvalidOperationException($\"FK {g.Key} has mismatched dependent columns.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Align HasColumnName across FKs that reuse a constraint name.","Give FKs with different property sets distinct constraint names.","Avoid renaming only one side of a shared FK property mapping."],"tags":["efcore","foreign-key","column-mapping","model-validation","duplicate"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}