{"record":{"id":"5184dfa1c1ed80b5","repo":"dotnet/efcore","slug":"the-foreign-keys-foreignkeyproperties1-on-enti-5184df","errorCode":null,"errorMessage":"The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but with different migration exclusion configurations.","messagePattern":"The foreign keys (.+?) on '(.+?)' and (.+?) on '(.+?)' are both mapped to '(.+?)\\.(.+?)', but with different migration exclusion configurations\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs","lineNumber":145,"sourceCode":"\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(),\n                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),\n                        foreignKey.GetConstraintName(storeObject, principalTable.Value)))\n                : false);\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public static string? GetConstraintName(\n        this IReadOnlyForeignKey foreignKey,","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L127-L163","documentation":"Thrown by AreCompatible when two foreign keys share name, tables, columns, uniqueness, and delete behavior, but differ in IsExcludedFromMigrations (one is excluded from migrations, the other is not). EF cannot decide whether to emit the constraint in migrations, so it rejects the ambiguity.","triggerScenarios":"Two sharing-table FKs with the same constraint name where one is configured IsExcludedFromMigrations(true) and the other uses the default false. Common when an FK is meant to be read-only/pre-existing (excluded) but shares a name with a managed one.","commonSituations":"Marking an FK as IsExcludedFromMigrations because it references a pre-existing constraint, while another shared-table relationship reuses the name without exclusion. Table-splitting setups where one side is hand-managed.","solutions":["Align IsExcludedFromMigrations: set both FKs to the same value (both excluded or both managed).","Give the FKs distinct HasConstraintName values when exclusion must differ.","Decide ownership: either let migrations manage the constraint or exclude both and manage manually.","Audit shared-table FKs for exclusion consistency before reusing names."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().HasOne(o => o.Customer)\n    .WithMany().HasForeignKey(o => o.CustomerId)\n    .HasConstraintName(\"FK_Order_Cust\");\nmodelBuilder.Entity<OrderHistory>().HasOne(h => h.Customer)\n    .WithMany().HasForeignKey(h => h.CustomerId)\n    .HasConstraintName(\"FK_Order_Cust\")\n    .IsExcludedFromMigrations(true); // shares table & name, differs\n\n// after\nmodelBuilder.Entity<OrderHistory>().HasOne(h => h.Customer)\n    .WithMany().HasForeignKey(h => h.CustomerId)\n    .HasConstraintName(\"FK_OrderHistory_Cust\")\n    .IsExcludedFromMigrations(true);","handlingStrategy":"validation","validationCode":"// Verify FKs sharing a name agree on IsExcludedFromMigrations\nforeach (var g in allFks.GroupBy(fk => fk.GetConstraintName()))\n{\n    if (g.Select(fk => fk.IsExcludedFromMigrations()).Distinct().Count() > 1)\n        throw new InvalidOperationException($\"FK {g.Key} has mismatched migration exclusion.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide ownership of each shared-table FK constraint (managed by migrations or excluded).","Apply IsExcludedFromMigrations consistently to FKs sharing a name.","When marking an FK read-only/pre-existing, also rename its constraint to avoid sharing."],"tags":["efcore","foreign-key","migrations","model-validation","duplicate"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}