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 as another seed entity mapped to the same table '{table}'. Key values should be unique across seed entities. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting values.

What it means

The non-sensitive variant of error 549: two seed entities mapped to the same table have identical key values, but because `EnableSensitiveDataLogging` is off the key value is omitted and the message points you to enable it to see the conflict.

Source

Thrown at src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:1936

                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));
                    command.EntityState = initialState;

                    identityMap.Add(key, command);
                }

                foreach (var columnMapping in mapping.ColumnMappings)
                {

View on GitHub (pinned to dbf9771522)

Solutions

  1. Re-run the operation with `optionsBuilder.EnableSensitiveDataLogging()` in a dev environment to see the exact conflicting key (see error 549 message).
  2. Make all seed key values unique across entities mapped to the shared table.
  3. Deduplicate/consolidate colliding `HasData` entries.

Example fix

// before
optionsBuilder.UseSqlServer(cs);
// diagnose
optionsBuilder.UseSqlServer(cs).EnableSensitiveDataLogging();
// fix: ensure unique seed keys across shared-table entities
Defensive patterns

Strategy: validation

Validate before calling

// Enable sensitive logging in a dev run to identify the duplicate key.
if (env.IsDevelopment()) optionsBuilder.EnableSensitiveDataLogging();
// Then deduplicate seed keys across shared-table entity types (see 549 validation).

Prevention

When it happens

Trigger: Same as 549 but with sensitive data logging disabled: `HasData` on multiple entity types sharing a table with colliding keys, then running migrations/EnsureCreated.

Common situations: Default (production) logging config collides on shared-table seed keys; inheritance hierarchies where derived types seed keys already used by the base type; merged seed modules.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/b7dc67f3464cf552. Report an issue: GitHub.