dotnet/efcore · error · InvalidOperationException
A seed entity for entity type '{entityType}' has the same ke
Error message
A seed entity for entity type '{entityType}' has the same key value {keyValue} as another seed entity mapped to the same table '{table}', but have different values for the column '{column}' - '{firstValue}', '{secondValue}'. What it means
Thrown by MigrationsModelDiffer (sensitive mode) when two seed entities for entity types mapped to the same table share a key value but assign DIFFERENT values to the same column. The differ cannot reconcile the conflict, so it reports both the column name and the two conflicting values.
Source
Thrown at src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:2010
if (column.DefaultValue != null)
{
value = column.DefaultValue;
}
else if (value == null
&& !column.IsNullable)
{
value = column.ProviderClrType.GetDefaultValue();
}
}
var existingColumnModification = command.ColumnModifications.FirstOrDefault(c => c.ColumnName == column.Name);
if (existingColumnModification != null)
{
if (!Equals(NormalizeSeedValue(existingColumnModification.Value), NormalizeSeedValue(value)))
{
if (sensitiveLoggingEnabled)
{
throw new InvalidOperationException(
RelationalStrings.ConflictingSeedValuesSensitive(
entityType.DisplayName(),
BuildValuesString(key),
table.SchemaQualifiedName,
existingColumnModification.ColumnName,
Convert.ToString(existingColumnModification.Value, CultureInfo.InvariantCulture),
Convert.ToString(value, CultureInfo.InvariantCulture)));
}
throw new InvalidOperationException(
RelationalStrings.ConflictingSeedValues(
entityType.DisplayName(),
table.SchemaQualifiedName,
existingColumnModification.ColumnName));
}
continue;
}View on GitHub (pinned to dbf9771522)
Solutions
- Make the conflicting column values identical across the two seed entries that share the key.
- Consolidate the two seed entries into one so the column has a single source of truth.
- If the rows are genuinely different, change the key so they no longer collide.
- Use sensitive logging to confirm the exact values, then align them.
Example fix
// before
modelBuilder.Entity<Base>().HasData(new Base { Id = 1, Name = "A" });
modelBuilder.Entity<Derived>().HasData(new Derived { Id = 1, Name = "B" });
// after
modelBuilder.Entity<Derived>().HasData(new Derived { Id = 1, Name = "A" }); Defensive patterns
Strategy: validation
Validate before calling
// For each shared-table seed group, assert no two rows with equal keys disagree on any column.
foreach (var group in SeedRows.GroupBy(r => (r.Table, r.Key)))
{
var first = group.First();
foreach (var row in group.Skip(1))
foreach (var col in first.Values.Keys)
if (!Equals(first.Values[col], row.Values[col]))
throw new InvalidOperationException($"Column '{col}' conflicts for key {group.Key.Key} on {group.Key.Table}.");
} Prevention
- Align non-key column values for any seed entries that share a key on a shared table.
- Consolidate shared-key seed entries into one statement.
- Use sensitive logging to confirm the exact conflicting values before fixing.
When it happens
Trigger: Multiple `HasData` calls (often across an inheritance hierarchy or shared-table mapping) produce rows with the same key but disagree on a non-key column value, while `EnableSensitiveDataLogging()` is on.
Common situations: TPH inheritance: base and derived seed sets disagree on a shared column; parallel teams seeding the same key with stale data; refactoring that split one seed into two without reconciling values.
Related errors
- A seed entity for entity type '{entityType}' has the same ke
- A seed entity for entity type '{entityType}' has the same ke
- A seed entity for entity type '{entityType}' has the same ke
- command.EntityState.ToString()
- The default value has not been specified for the column '{ta
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/97e9ce03a9f1dc74.
Report an issue: GitHub.