{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L99-L135","documentation":"Two foreign keys share a constraint name and table but one is configured as unique and the other as non-unique. The relational layer cannot map two structurally different constraints to one database object.","triggerScenarios":"RelationalForeignKeyExtensions.AreCompatible with shouldThrow:true when foreignKey.IsUnique != duplicateForeignKey.IsUnique. Typical with shared ownership, TPT, or table-splitting where one path is 1:1 (unique) and another is 1:* (non-unique) but both FKs resolve to the same constraint name.","commonSituations":"An owned entity also participates in a regular one-to-many relationship using the same FK columns; an alternate key relationship defined alongside a primary FK; explicit HasConstraintName reuse across mismatched relationships.","solutions":["Make both FKs agree on uniqueness (both unique or both non-unique) by changing the relationship multiplicity on one side.","Assign distinct constraint names with HasConstraintName so the two FKs map to separate database constraints.","Remap one relationship to different columns so it no longer collides.","Drop the redundant FK if both relationships are semantically the same."],"exampleFix":"// before: owned (unique) FK collides with a separate non-unique FK on the same columns\nmodelBuilder.Entity<User>().OwnsOne(u => u.Profile);\nmodelBuilder.Entity<User>().HasMany(u => u.Profiles).WithOne().HasForeignKey(p => p.UserId);\n\n// after: keep one relationship; remove the conflicting second FK or rename it\nmodelBuilder.Entity<User>().OwnsOne(u => u.Profile);\n// or, if both are needed, give the second a distinct constraint name\nmodelBuilder.Entity<User>().HasMany(u => u.Profiles).WithOne().HasForeignKey(p => p.UserId)\n    .HasConstraintName(\"FK_Profile_User_Many\");","handlingStrategy":"validation","validationCode":"// Detect uniqueness mismatch before model validation throws.\nvar fks = modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys());\nforeach (var fk in fks)\n{\n    var name = fk.GetConstraintName();\n    foreach (var dup in fks.Where(o => o != fk && o.GetConstraintName() == name\n                                       && o.DeclaringEntityType.GetSchemaQualifiedTableName() == fk.DeclaringEntityType.GetSchemaQualifiedTableName()))\n    {\n        if (fk.IsUnique != dup.IsUnique)\n            Console.WriteLine($\"Uniqueness mismatch on constraint '{name}'.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide relationship multiplicity (1:1 vs 1:*) up front and apply it consistently to all FKs that may share a constraint name.","Avoid declaring both an ownership and a regular relationship on the same FK columns.","Validate the model in tests before runtime."],"tags":["ef-core","relational-metadata","foreign-key","uniqueness","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}