{"record":{"id":"4815c2649fd40acf","repo":"dotnet/efcore","slug":"a-seed-entity-for-entity-type-entitytype-has-t","errorCode":null,"errorMessage":"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.","messagePattern":"A seed entity for entity type '(.+?)' has the same key value (.+?) as another seed entity mapped to the same table '(.+?)'\\. Key values should be unique across seed entities\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs","lineNumber":1929,"sourceCode":"                    var valueConverter = columnMapping.TypeMapping.Converter;\n                    key[i] = NormalizeSeedValue(\n                        valueConverter == null\n                            ? value\n                            : valueConverter.ConvertToProvider(value));\n                }\n\n                if (!keyFound)\n                {\n                    continue;\n                }\n\n                if (identityMap.FindCommand(key) is { } existingCommand)\n                {\n                    if (!table.IsShared)\n                    {\n                        if (sensitiveLoggingEnabled)\n                        {\n                            throw new InvalidOperationException(\n                                RelationalStrings.DuplicateSeedDataSensitive(\n                                    entityType.DisplayName(),\n                                    BuildValuesString(key),\n                                    table.SchemaQualifiedName));\n                        }\n\n                        throw new InvalidOperationException(\n                            RelationalStrings.DuplicateSeedData(\n                                entityType.DisplayName(),\n                                table.SchemaQualifiedName));\n                    }\n\n                    command = existingCommand;\n                }\n                else\n                {\n                    command = CommandBatchPreparerDependencies.ModificationCommandFactory.CreateNonTrackedModificationCommand(\n                        new NonTrackedModificationCommandParameters(table, sensitiveLoggingEnabled));","sourceCodeStart":1911,"sourceCodeEnd":1947,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs#L1911-L1947","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nmodelBuilder.Entity<A>().HasData(new A { Id = 1 });\nmodelBuilder.Entity<B>().HasData(new B { Id = 1 }); // A and B share a table\n// after\nmodelBuilder.Entity<A>().HasData(new A { Id = 1 });\nmodelBuilder.Entity<B>().HasData(new B { Id = 2 });","handlingStrategy":"validation","validationCode":"// Detect duplicate seed keys across entity types mapped to the same table BEFORE migrating.\nvar byTable = new Dictionary<string, HashSet<object>>();\nforeach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var table = et.GetTableName();\n    foreach (var seed in et.GetSeedData())\n    {\n        var key = seed.Values.First(); // simplified: use full key\n        var set = byTable.GetOrAdd(table, _ => new HashSet<object>());\n        if (!set.Add(key)) throw new InvalidOperationException($\"Duplicate seed key on table {table}.\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["efcore","migrations","seed-data","model-differ","sensitive-data","relational"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}