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}'. Key values should be unique across seed entities. What it means
Thrown by MigrationsModelDiffer (with sensitive data logging enabled) when two seed data entries for entity types mapped to the SAME table share the same key value. The differ detects the duplicate key while building seed operations and refuses to emit ambiguous inserts. Sensitive mode includes the actual key value in the message.
Source
Thrown at src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:1929
var valueConverter = columnMapping.TypeMapping.Converter;
key[i] = NormalizeSeedValue(
valueConverter == null
? value
: valueConverter.ConvertToProvider(value));
}
if (!keyFound)
{
continue;
}
if (identityMap.FindCommand(key) is { } existingCommand)
{
if (!table.IsShared)
{
if (sensitiveLoggingEnabled)
{
throw new InvalidOperationException(
RelationalStrings.DuplicateSeedDataSensitive(
entityType.DisplayName(),
BuildValuesString(key),
table.SchemaQualifiedName));
}
throw new InvalidOperationException(
RelationalStrings.DuplicateSeedData(
entityType.DisplayName(),
table.SchemaQualifiedName));
}
command = existingCommand;
}
else
{
command = CommandBatchPreparerDependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(
new NonTrackedModificationCommandParameters(table, sensitiveLoggingEnabled));View on GitHub (pinned to dbf9771522)
Solutions
- Make seed key values unique across every entity type mapped to the shared table.
- Deduplicate seed data: remove one of the colliding `HasData` entries.
- If the rows are meant to be the same, consolidate into a single seed statement.
- Temporarily disable sensitive logging to confirm count, then fix the specific keys.
Example fix
// before
modelBuilder.Entity<A>().HasData(new A { Id = 1 });
modelBuilder.Entity<B>().HasData(new B { Id = 1 }); // A and B share a table
// after
modelBuilder.Entity<A>().HasData(new A { Id = 1 });
modelBuilder.Entity<B>().HasData(new B { Id = 2 }); Defensive patterns
Strategy: validation
Validate before calling
// Detect duplicate seed keys across entity types mapped to the same table BEFORE migrating.
var byTable = new Dictionary<string, HashSet<object>>();
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
var table = et.GetTableName();
foreach (var seed in et.GetSeedData())
{
var key = seed.Values.First(); // simplified: use full key
var set = byTable.GetOrAdd(table, _ => new HashSet<object>());
if (!set.Add(key)) throw new InvalidOperationException($"Duplicate seed key on table {table}.");
}
} Prevention
- Keep a single source of truth for seed data per table.
- For inheritance/shared-table mappings, ensure derived-type seed keys never overlap base-type seed keys.
- Enable `EnableSensitiveDataLogging` in dev to surface the exact conflicting key.
When it happens
Trigger: Calling `HasData` (or `Entity<T>().HasData`) for two entity types that share a table (TPH/TPT mapping) with overlapping key values, then running `Migrate`/`EnsureCreated`/`ScriptMigration` with `EnableSensitiveDataLogging()` on.
Common situations: Two entities mapped to one table (inheritance or explicit `ToTable`) both seeding key `1`; copy-pasted seed data; merging seed lists from multiple modules that collide on keys.
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/4815c2649fd40acf.
Report an issue: GitHub.