{"record":{"id":"86f1220ceb3cd98f","repo":"dotnet/efcore","slug":"the-foreign-keys-foreignkeyproperties1-on-enti-86f122","errorCode":null,"errorMessage":"The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but configured with different delete behavior ('{deleteBehavior1}' and '{deleteBehavior2}').","messagePattern":"The foreign keys (.+?) on '(.+?)' and (.+?) on '(.+?)' are both mapped to '(.+?)\\.(.+?)', but configured with different delete behavior \\('(.+?)' and '(.+?)'\\)\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs","lineNumber":132,"sourceCode":"        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(),\n                        duplicateForeignKey.Properties.Format(),\n                        duplicateForeignKey.DeclaringEntityType.DisplayName(),\n                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                        foreignKey.GetConstraintName(storeObject, principalTable.Value),\n                        referentialAction,\n                        duplicateReferentialAction))\n                : false\n            : foreignKey.IsExcludedFromMigrations() == duplicateForeignKey.IsExcludedFromMigrations()\n            || (shouldThrow\n                ? throw new InvalidOperationException(\n                    RelationalStrings.DuplicateForeignKeyExcludedFromMigrationsMismatch(\n                        foreignKey.Properties.Format(),\n                        foreignKey.DeclaringEntityType.DisplayName(),\n                        duplicateForeignKey.Properties.Format(),\n                        duplicateForeignKey.DeclaringEntityType.DisplayName(),","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L114-L150","documentation":"Two foreign keys share a constraint name and table but have different DeleteBehavior values (translated to different referential actions such as Cascade vs Restrict). The database constraint can only encode one action.","triggerScenarios":"RelationalForeignKeyExtensions.AreCompatible with shouldThrow:true when RelationalModel.ToReferentialAction(foreignKey.DeleteBehavior) != ToReferentialAction(duplicateForeignKey.DeleteBehavior). Occurs when two relationships share a constraint name but one sets OnDelete(Cascade) and the other OnDelete(NoAction) (or ClientSetNull/Restrict).","commonSituations":"An owned entity and a regular reference to the same principal both ending up on the same columns; manually setting OnDelete differently on two relationships that default to the same constraint name; migration scaffolding after a DeleteBehavior change that conflicts with a pre-existing constraint.","solutions":["Align both FKs to the same OnDelete behavior.","Give the FKs distinct constraint names via HasConstraintName.","Remove or remap the redundant FK if only one relationship is intended.","If the duplicate is from TPT/splitting, consolidate the relationship definitions so a single DeleteBehavior applies."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasOne(o => o.Customer).WithMany().OnDelete(DeleteBehavior.Cascade);\nmodelBuilder.Entity<Refund>().HasOne(r => r.Customer).WithMany().OnDelete(DeleteBehavior.NoAction);\n// both default to the same constraint name -> mismatch\n\n// after: align behaviors or rename\nmodelBuilder.Entity<Order>().HasOne(o => o.Customer).WithMany().OnDelete(DeleteBehavior.Cascade);\nmodelBuilder.Entity<Refund>().HasOne(r => r.Customer).WithMany().OnDelete(DeleteBehavior.Cascade);","handlingStrategy":"validation","validationCode":"// Verify DeleteBehavior consistency across colliding FKs.\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    {\n        if (fk.DeleteBehavior != dup.DeleteBehavior)\n            Console.WriteLine($\"DeleteBehavior mismatch on '{name}': {fk.DeleteBehavior} vs {dup.DeleteBehavior}.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize DeleteBehavior across relationships to the same principal when they may share constraints.","Review OnDelete calls during code review for consistency.","Test model finalization in CI."],"tags":["ef-core","relational-metadata","foreign-key","delete-behavior","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}