{"record":{"id":"db7df2b0c1bdf486","repo":"dotnet/efcore","slug":"the-foreign-keys-foreignkeyproperties1-on-enti-db7df2","errorCode":null,"errorMessage":"The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but with different uniqueness configurations.","messagePattern":"The foreign keys (.+?) on '(.+?)' and (.+?) on '(.+?)' are both mapped to '(.+?)\\.(.+?)', but with different uniqueness configurations\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs","lineNumber":117,"sourceCode":"        {\n            return shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyPrincipalColumnMismatch(\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.PrincipalKey.Properties.FormatColumns(principalTable.Value),\n                        duplicateForeignKey.PrincipalKey.Properties.FormatColumns(principalTable.Value)))\n                : false;\n        }\n\n        if (foreignKey.IsUnique != duplicateForeignKey.IsUnique)\n        {\n            return shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyUniquenessMismatch(\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                : false;\n        }\n\n        var referentialAction = RelationalModel.ToReferentialAction(foreignKey.DeleteBehavior);\n        var duplicateReferentialAction = RelationalModel.ToReferentialAction(duplicateForeignKey.DeleteBehavior);\n        return referentialAction != duplicateReferentialAction\n            ? shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyDeleteBehaviorMismatch(\n                        foreignKey.Properties.Format(),\n                        foreignKey.DeclaringEntityType.DisplayName(),","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L99-L135","documentation":"Thrown by AreCompatible when two foreign keys share name and tables/columns but have different uniqueness (one IsUnique true, the other false). The database FK constraint cannot be both unique and non-unique, so EF rejects the conflict.","triggerScenarios":"Two sharing-table FKs resolving to one constraint name where one is configured IsUnique(true) (e.g. a 1:1 relationship) and the other is the default non-unique (1:many).","commonSituations":"TPH/table-splitting where base and derived entity relationships over the same FK differ in cardinality. Adding a 1:1 relationship that reuses a name from an existing 1:many FK.","solutions":["Align the IsUnique values: if the constraint should be unique, make both FKs unique; otherwise neither.","Give the FKs distinct HasConstraintName values when cardinality genuinely differs.","Reconcile relationship cardinality (HasOne/WithOne vs HasOne/WithMany) across the colliding FKs.","Ensure shared-table relationships are intentionally consistent before reusing a name."],"exampleFix":"// before\nmodelBuilder.Entity<User>().HasOne(u => u.Profile)\n    .WithOne(p => p.User).HasForeignKey<Profile>(p => p.UserId)\n    .HasConstraintName(\"FK_Profile_User\"); // unique\nmodelBuilder.Entity<AuditLog>().HasOne(a => a.User)\n    .WithMany(u => u.AuditLogs).HasForeignKey(a => a.UserId)\n    .HasConstraintName(\"FK_Profile_User\"); // non-unique, shares table\n\n// after\nmodelBuilder.Entity<AuditLog>().HasOne(a => a.User)\n    .WithMany(u => u.AuditLogs).HasForeignKey(a => a.UserId)\n    .HasConstraintName(\"FK_AuditLog_User\");","handlingStrategy":"validation","validationCode":"// Verify FKs sharing a name have the same uniqueness\nforeach (var g in allFks.GroupBy(fk => fk.GetConstraintName()))\n{\n    if (g.Select(fk => fk.IsUnique).Distinct().Count() > 1)\n        throw new InvalidOperationException($\"FK {g.Key} has mismatched uniqueness.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not mix 1:1 and 1:many relationships under the same FK constraint name.","When changing cardinality, assign a fresh HasConstraintName.","Validate the model in tests to catch uniqueness conflicts."],"tags":["efcore","foreign-key","uniqueness","relationship","model-validation","duplicate"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}