{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs#L127-L163","documentation":"Two foreign keys share a constraint name and table but one is excluded from migrations and the other is not. The migration generator cannot decide whether to emit the constraint.","triggerScenarios":"RelationalForeignKeyExtensions.AreCompatible final check when foreignKey.IsExcludedFromMigrations() != duplicateForeignKey.IsExcludedFromMigrations(). Triggered when one FK calls IsExcludedFromMigrations(true) (or is on a table mapped ToTable with migrations excluded) and a colliding FK on the same name does not.","commonSituations":"Mixing migration-excluded (pre-created) tables with regular ones in the same schema; an entity mapped both to a view and to a table where one FK is excluded; partial migrations where some constraints are pre-provisioned in SQL.","solutions":["Set the same IsExcludedFromMigrations value on both FKs.","Assign distinct constraint names so only one is tied to the excluded table.","Move the excluded entity to a separate schema/table so its FKs cannot collide with migration-managed ones.","Remove the duplicate FK."],"exampleFix":"// before\nmodelBuilder.Entity<A>().HasOne(a => a.B).WithMany().HasForeignKey(a => a.BId)\n    .HasConstraintName(\"FK_AB\");\nmodelBuilder.Entity<A>().ToTable(\"A\", t => t.ExcludeFromMigrations());\n// the colliding FK on B side is not excluded -> mismatch\n\n// after: make both consistent\nmodelBuilder.Entity<A>().HasOne(a => a.B).WithMany().HasForeignKey(a => a.BId)\n    .HasConstraintName(\"FK_AB\").Metadata.SetIsExcludedFromMigrations(true);","handlingStrategy":"validation","validationCode":"// Ensure colliding FKs agree on migration exclusion.\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.IsExcludedFromMigrations() != dup.IsExcludedFromMigrations())\n            Console.WriteLine($\"Migration-exclusion mismatch on constraint '{name}'.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When using pre-provisioned schema (ExcludeFromMigrations), apply the exclusion consistently to all related FKs.","Document which entities are migration-managed vs pre-created.","Add a model-validation test to CI."],"tags":["ef-core","relational-metadata","foreign-key","migrations","model-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}