{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs#L1911-L1947","documentation":"During migration diffing, EF builds an identity map keyed by primary key per target table. For a non-shared table, encountering two seed entities with the same key is ambiguous and throws DuplicateSeedData, because the diff could not decide which seed wins.","triggerScenarios":"Two HasData calls (or one with two objects) seeding the same entity type with identical key values, both mapped to the same non-shared table; or re-seeding after a key change left duplicates.","commonSituations":"Copy-pasted seed rows; merging seed data from multiple configuration sources; key generators producing collisions; version upgrades that changed key computation (value converters) so previously-distinct seeds now collide.","solutions":["Dedupe seed data: ensure each key value appears exactly once per entity type per table.","When merging seeds from multiple modules, run a union/dedup pass keyed by primary key before HasData.","Enable sensitive data logging to see the offending key value (DuplicateSeedDataSensitive message includes it).","Re-run Add-Migration after correcting the seed data so the diff is regenerated."],"exampleFix":"// before\nmodelBuilder.Entity<User>().HasData(\n    new User { Id = 1, Name = \"Alice\" },\n    new User { Id = 1, Name = \"Bob\" }); // duplicate key 1\n\n// after\nmodelBuilder.Entity<User>().HasData(\n    new User { Id = 1, Name = \"Alice\" },\n    new User { Id = 2, Name = \"Bob\" });","handlingStrategy":"validation","validationCode":"// Dedupe seed data by primary key before calling HasData.\nvar deduped = seedUsers\n    .GroupBy(u => u.Id)\n    .Select(g => g.First())\n    .ToArray();\nmodelBuilder.Entity<User>().HasData(deduped);","typeGuard":"static bool HasUniqueKeys<T, TKey>(IEnumerable<T> rows, Func<T, TKey> keySelector)\n    => rows.GroupBy(keySelector).All(g => g.Count() == 1);","tryCatchPattern":null,"preventionTips":["Dedupe seed collections by key before HasData.","Enable sensitive data logging during development to surface the colliding key value.","Merge seeds from multiple modules with a union-by-key step."],"tags":["ef-core","migrations","seed-data","data-seeding","primary-key"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}