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 use different columns ({columnNames1} and {columnNames2}). What it means
Two foreign keys resolve to the same constraint name on the same table and reference the same principal table, but the actual column sets they use differ, so they cannot be a single FK. AreCompatible throws DuplicateForeignKeyColumnMismatch when the dependent or principal column name lists are not equal.
Solutions
- Compare the column lists named in the message and decide whether they represent the same relationship or two distinct ones.
- If distinct, give each FK a unique HasConstraintName (or remove the override to let EF compute names from properties).
- If they should be the same, align the properties configured via HasForeignKey on both relationships.
Example fix
// before
modelBuilder.Entity<Payment>()
.HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerId)
.HasConstraintName("FK_Payment_Customer");
modelBuilder.Entity<Payment>()
.HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerCode)
.HasConstraintName("FK_Payment_Customer");
// after
modelBuilder.Entity<Payment>()
.HasOne(p => p.Customer).WithMany().HasForeignKey(p => p.CustomerCode)
.HasConstraintName("FK_Payment_Customer");
// remove the conflicting duplicate FK declaration Defensive patterns
Strategy: validation
Validate before calling
// Ensure FKs sharing a name on the same table use the same column set
var conflicts = modelBuilder.Model.GetEntityTypes()
.SelectMany(e => e.GetDeclaredForeignKeys())
.GroupBy(fk => (fk.GetConstraintName(), fk.DeclaringEntityType.GetSchemaQualifiedTableName()))
.Where(g =>
{
var sets = g.Select(fk => string.Join(",", fk.Properties.Select(p => p.GetColumnName()))).Distinct();
return sets.Count() > 1;
});
if (conflicts.Any()) throw new InvalidOperationException("FKs sharing a constraint name use different columns."); Prevention
- When overriding HasConstraintName, ensure the HasForeignKey column lists are identical across all relationships that should share the name.
- Add a startup test that asserts same-named FKs on a table use identical dependent and principal column sets.
When it happens
Trigger: Two relationships on the same entity both given the same HasConstraintName but using different FK property columns; composite FKs where one half of a shared name differs.
Common situations: Refactoring a relationship to use different properties (e.g. switching from CustomerId to CustomerCode) without updating or removing an explicit constraint name; multiple navigations to the same principal.
Related errors
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
- ' . ' and ' . ' are both mapped to column ' ' in ' ', but…
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/ae3e4d12d31097d3.
Report an issue: GitHub.
Appendix: 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 3a2006ef56)