dotnet/efcore · error · InvalidOperationException

A seed entity for entity type

Error message

A seed entity for entity type '{entityType}' has the same key value as another seed entity mapped to the same table '{table}', but have different values for the column '{column}'. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting values.

What it means

Non-sensitive variant of the conflicting-seed-values check: same key, same table, but two seed entries disagree on a non-key column. The column name is reported but the values are hidden; turn on EnableSensitiveDataLogging to see them. Same root cause as error 521.

Solutions

  1. Temporarily enable optionsBuilder.EnableSensitiveDataLogging() to surface the exact key and both values (error 521), then disable it again.
  2. Search HasData blocks for the reported entity type and reconcile the conflicting column.
  3. Ensure only one seed entry exists per key per table.
  4. Regenerate the migration after the fix.

Example fix

// before
modelBuilder.Entity<Product>().HasData(
    new Product { Id = 7, Sku = "A1" },
    new Product { Id = 7, Sku = "B2" });

// after
modelBuilder.Entity<Product>().HasData(
    new Product { Id = 7, Sku = "A1" });
Defensive patterns

Strategy: validation

Validate before calling

// Reuse the AssertSeedConsistent helper from 521; run it in a unit test
// against the model's configured seed data, with sensitive logging OFF in prod.
[Test] public void Seed_Data_Is_Consistent()
    => AssertSeedConsistent(_seedCustomers, c => c.Id, c => c.Sku, c => c.Name);

Type guard

null

Try / catch

// In tooling, catch and re-run with sensitive logging for diagnostics
try { db.Database.Migrate(); }
catch (InvalidOperationException ex)
    when (ex.Message.Contains("same key value"))
{
    using var sensitive = new DbContextOptionsBuilder<Db>()
        .UseSqlServer(conn).EnableSensitiveDataLogging().Options;
    using var diag = new Db(sensitive);
    diag.Database.Migrate(); // surfaces exact values
    throw;
}

Prevention

When it happens

Trigger: HasData contains two entries with identical keys but a differing value for some mapped column, while sensitive data logging is OFF. Reached during model diffing for migrations or any code that builds seed commands.

Common situations: Default state in production where EnableSensitiveDataLogging is (correctly) disabled; copy-paste of seed entries with one field edited; refactor that split one seed source into two without deduplicating.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/fed4a1c1eaea0f83. Report an issue: GitHub.

Appendix: source

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

                    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;
                    }

                    writeValue = writeValue
                        && initialState != EntityState.Deleted
                        && property.GetBeforeSaveBehavior() == PropertySaveBehavior.Save;

                    command.AddColumnModification(
                        new ColumnModificationParameters(
                            column, originalValue: value, value, property, columnMapping.TypeMapping,
                            read: false, write: writeValue,
                            key: property.IsPrimaryKey(), condition: false,

View on GitHub (pinned to 3a2006ef56)