{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L67-L103","documentation":"Two foreign keys resolve to the same constraint name on the same table and reference the same principal table, but the actual column sets they use differ, so they cannot be a single FK. AreCompatible throws DuplicateForeignKeyColumnMismatch when the dependent or principal column name lists are not equal.","triggerScenarios":"Two relationships on the same entity both given the same HasConstraintName but using different FK property columns; composite FKs where one half of a shared name differs.","commonSituations":"Refactoring a relationship to use different properties (e.g. switching from CustomerId to CustomerCode) without updating or removing an explicit constraint name; multiple navigations to the same principal.","solutions":["Compare the column lists named in the message and decide whether they represent the same relationship or two distinct ones.","If distinct, give each FK a unique HasConstraintName (or remove the override to let EF compute names from properties).","If they should be the same, align the properties configured via HasForeignKey on both relationships."],"exampleFix":"// before\nmodelBuilder.Entity<Payment>()\n    .HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerId)\n    .HasConstraintName(\"FK_Payment_Customer\");\nmodelBuilder.Entity<Payment>()\n    .HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerCode)\n    .HasConstraintName(\"FK_Payment_Customer\");\n\n// after\nmodelBuilder.Entity<Payment>()\n    .HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerCode)\n    .HasConstraintName(\"FK_Payment_Customer\");\n// remove the conflicting duplicate FK declaration","handlingStrategy":"validation","validationCode":"// Ensure FKs sharing a name on the same table use the same column set\nvar conflicts = modelBuilder.Model.GetEntityTypes()\n    .SelectMany(e => e.GetDeclaredForeignKeys())\n    .GroupBy(fk => (fk.GetConstraintName(), fk.DeclaringEntityType.GetSchemaQualifiedTableName()))\n    .Where(g =>\n    {\n        var sets = g.Select(fk => string.Join(\",\", fk.Properties.Select(p => p.GetColumnName()))).Distinct();\n        return sets.Count() > 1;\n    });\nif (conflicts.Any()) throw new InvalidOperationException(\"FKs sharing a constraint name use different columns.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When overriding HasConstraintName, ensure the HasForeignKey column lists are identical across all relationships that should share the name.","Add a startup test that asserts same-named FKs on a table use identical dependent and principal column sets."],"tags":["relational","foreign-keys","model-validation","column-mapping"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}