dotnet/efcore · error · InvalidOperationException

The foreign keys on ' ' and on ' ' are both mapped to ' …

Error message

The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but with different migration exclusion configurations.

What it means

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.

Solutions

  1. Set the same IsExcludedFromMigrations value on both FKs.
  2. Assign distinct constraint names so only one is tied to the excluded table.
  3. Move the excluded entity to a separate schema/table so its FKs cannot collide with migration-managed ones.
  4. Remove the duplicate FK.

Example fix

// before
modelBuilder.Entity<A>().HasOne(a => a.B).WithMany().HasForeignKey(a => a.BId)
    .HasConstraintName("FK_AB");
modelBuilder.Entity<A>().ToTable("A", t => t.ExcludeFromMigrations());
// the colliding FK on B side is not excluded -> mismatch

// after: make both consistent
modelBuilder.Entity<A>().HasOne(a => a.B).WithMany().HasForeignKey(a => a.BId)
    .HasConstraintName("FK_AB").Metadata.SetIsExcludedFromMigrations(true);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure colliding FKs agree on migration exclusion.
var fks = modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys());
foreach (var fk in fks)
{
    var name = fk.GetConstraintName();
    foreach (var dup in fks.Where(o => o != fk && o.GetConstraintName() == name))
    {
        if (fk.IsExcludedFromMigrations() != dup.IsExcludedFromMigrations())
            Console.WriteLine($"Migration-exclusion mismatch on constraint '{name}'.");
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/5184dfa1c1ed80b5. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs:145

        var referentialAction = RelationalModel.ToReferentialAction(foreignKey.DeleteBehavior);
        var duplicateReferentialAction = RelationalModel.ToReferentialAction(duplicateForeignKey.DeleteBehavior);
        return referentialAction != duplicateReferentialAction
            ? shouldThrow
                ? throw new InvalidOperationException(
                    RelationalStrings.DuplicateForeignKeyDeleteBehaviorMismatch(
                        foreignKey.Properties.Format(),
                        foreignKey.DeclaringEntityType.DisplayName(),
                        duplicateForeignKey.Properties.Format(),
                        duplicateForeignKey.DeclaringEntityType.DisplayName(),
                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),
                        foreignKey.GetConstraintName(storeObject, principalTable.Value),
                        referentialAction,
                        duplicateReferentialAction))
                : false
            : foreignKey.IsExcludedFromMigrations() == duplicateForeignKey.IsExcludedFromMigrations()
            || (shouldThrow
                ? throw new InvalidOperationException(
                    RelationalStrings.DuplicateForeignKeyExcludedFromMigrationsMismatch(
                        foreignKey.Properties.Format(),
                        foreignKey.DeclaringEntityType.DisplayName(),
                        duplicateForeignKey.Properties.Format(),
                        duplicateForeignKey.DeclaringEntityType.DisplayName(),
                        foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),
                        foreignKey.GetConstraintName(storeObject, principalTable.Value)))
                : false);
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public static string? GetConstraintName(
        this IReadOnlyForeignKey foreignKey,

View on GitHub (pinned to 3a2006ef56)