dotnet/efcore · error · InvalidOperationException
The foreign keys {foreignKeyProperties1} on '{entityType1}'
Error message
The foreign keys {foreignKeyProperties1} on '{entityType1}' and {foreignKeyProperties2} on '{entityType2}' are both mapped to '{table}.{foreignKeyName}', but use different columns ({columnNames1} and {columnNames2}). What it means
Thrown by AreCompatible when two foreign keys share name, dependent table, and principal table, but the dependent-side column names differ. One database FK constraint must reference a fixed set of dependent columns, so divergent column sets cannot be unified.
Source
Thrown at src/EFCore.Relational/Metadata/Internal/RelationalForeignKeyExtensions.cs:85
? throw new InvalidOperationException(
RelationalStrings.DuplicateForeignKeyPrincipalTableMismatch(
foreignKey.Properties.Format(),
foreignKey.DeclaringEntityType.DisplayName(),
duplicateForeignKey.Properties.Format(),
duplicateForeignKey.DeclaringEntityType.DisplayName(),
foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),
principalTable.HasValue
? foreignKey.GetConstraintName(storeObject, principalTable.Value)
: foreignKey.GetDefaultName(),
principalType.GetSchemaQualifiedTableName(),
duplicatePrincipalType.GetSchemaQualifiedTableName()))
: false;
}
if (!columnNames.SequenceEqual(duplicateColumnNames))
{
return shouldThrow
? throw new InvalidOperationException(
RelationalStrings.DuplicateForeignKeyColumnMismatch(
foreignKey.Properties.Format(),
foreignKey.DeclaringEntityType.DisplayName(),
duplicateForeignKey.Properties.Format(),
duplicateForeignKey.DeclaringEntityType.DisplayName(),
foreignKey.DeclaringEntityType.GetSchemaQualifiedTableName(),
foreignKey.GetConstraintName(storeObject, principalTable.Value),
foreignKey.Properties.FormatColumns(storeObject),
duplicateForeignKey.Properties.FormatColumns(storeObject)))
: false;
}
if (!principalColumns.SequenceEqual(duplicatePrincipalColumns))
{
return shouldThrow
? throw new InvalidOperationException(
RelationalStrings.DuplicateForeignKeyPrincipalColumnMismatch(
foreignKey.Properties.Format(),View on GitHub (pinned to dbf9771522)
Solutions
- Map the colliding FK properties to the same column names (align HasColumnName).
- Give the FKs distinct constraint names so each owns its column set.
- Ensure the FK properties themselves are the same set when they are meant to be one constraint.
- Audit the dependent column names of each colliding FK with GetColumnNames(storeObject).
Example fix
// before (two FKs same name, dependent columns OrderId vs OrderRef)
modelBuilder.Entity<Allocation>().HasOne(a => a.Order)
.WithMany().HasForeignKey(a => a.OrderId).HasConstraintName("FK_Alloc_Ref");
modelBuilder.Entity<Allocation>().HasOne(a => a.Order)
.WithMany().HasForeignKey(a => a.OrderRef).HasConstraintName("FK_Alloc_Ref")
.OnDelete(DeleteBehavior.Restrict);
// after (unify columns or rename one constraint)
modelBuilder.Entity<Allocation>().Property(a => a.OrderRef).HasColumnName("OrderId");
// or give the second FK a distinct name Defensive patterns
Strategy: validation
Validate before calling
// Verify FKs sharing a name map to identical dependent column names
foreach (var g in allFks.GroupBy(fk => fk.GetConstraintName()))
{
var colSets = g.Select(fk => string.Join(",", fk.Properties.GetColumnNames(store))).Distinct();
if (colSets.Count() > 1)
throw new InvalidOperationException($"FK {g.Key} has mismatched dependent columns.");
} Prevention
- Align HasColumnName across FKs that reuse a constraint name.
- Give FKs with different property sets distinct constraint names.
- Avoid renaming only one side of a shared FK property mapping.
When it happens
Trigger: Two sharing-table FKs with the same constraint name map their FK properties to different column names on the shared table. Happens when properties are renamed via HasColumnName on one but not the other, or when two FKs reuse a name but use different property sets.
Common situations: TPH hierarchy where base and derived define FKs resolving to one name, but the column mappings diverge. Column renaming in migrations without updating FK config.
Related errors
- The foreign keys {foreignKeyProperties1} on '{entityType1}'
- The foreign keys {foreignKeyProperties1} on '{entityType1}'
- The foreign keys {foreignKeyProperties1} on '{entityType1}'
- The foreign keys {foreignKeyProperties1} on '{entityType1}'
- The foreign keys {foreignKeyProperties1} on '{entityType1}'
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/ae3e4d12d31097d3.
Report an issue: GitHub.