{"record":{"id":"55ba42d08df01561","repo":"dotnet/efcore","slug":"the-name-migrationname-is-used-by-an-existing","errorCode":null,"errorMessage":"The name '{migrationName}' is used by an existing migration.","messagePattern":"The name '(.+?)' is used by an existing migration\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs","lineNumber":80,"sourceCode":"    /// </param>\n    /// <param name=\"language\">The project's language.</param>\n    /// <param name=\"dryRun\">If <see langword=\"true\" />, then nothing is actually written to disk.</param>\n    /// <returns>The scaffolded migration.</returns>\n    public virtual ScaffoldedMigration ScaffoldMigration(\n        string migrationName,\n        string? rootNamespace,\n        string? subNamespace = null,\n        string? language = null,\n        bool dryRun = false)\n    {\n        if (string.Equals(migrationName, \"migration\", StringComparison.OrdinalIgnoreCase))\n        {\n            throw new OperationException(DesignStrings.CircularBaseClassDependency);\n        }\n\n        if (Dependencies.MigrationsAssembly.FindMigrationId(migrationName) != null)\n        {\n            throw new OperationException(DesignStrings.DuplicateMigrationName(migrationName));\n        }\n\n        var overrideNamespace = rootNamespace == null;\n        var subNamespaceDefaulted = false;\n        if (string.IsNullOrEmpty(subNamespace) && !overrideNamespace)\n        {\n            subNamespaceDefaulted = true;\n            subNamespace = \"Migrations\";\n        }\n\n        var (key, typeInfo) = Dependencies.MigrationsAssembly.Migrations.LastOrDefault();\n\n        var migrationNamespace =\n            (!string.IsNullOrEmpty(rootNamespace)\n                && !string.IsNullOrEmpty(subNamespace))\n                ? rootNamespace + \".\" + subNamespace\n                : !string.IsNullOrEmpty(rootNamespace)\n                    ? rootNamespace","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs#L62-L98","documentation":"Before scaffolding, `MigrationsScaffolder.ScaffoldMigration` calls `MigrationsAssembly.FindMigrationId(migrationName)`; if a migration with that name already exists in the configured migrations assembly it throws `OperationException(DuplicateMigrationName)`. EF requires migration names to be unique within an assembly so that `GetId()` lookups and `__EFMigrationsHistory` entries stay unambiguous.","triggerScenarios":"`dotnet ef migrations add InitialCreate` when an `InitialCreate` migration class already exists in the migrations assembly; calling `ScaffoldMigration` with a name already produced by `MigrationsIdGenerator.GenerateId`.","commonSituations":"Re-running a command after a partial/failed previous run left the class behind, merging branches that each added a same-named migration, or forgetting a migration was already added on another machine.","solutions":["Pick a new, descriptive name for the migration.","If the existing one is stale/unwanted, remove it first with `dotnet ef migrations remove` (only if unapplied) or delete the files manually and regenerate.","If two branches collided, rename one migration's class, file, and timestamp prefix, or re-add with a distinct name."],"exampleFix":"// before\ndotnet ef migrations add InitialCreate   // already exists\n// after\ndotnet ef migrations add AddUsersTable","handlingStrategy":"validation","validationCode":"// Check the migrations assembly for an existing name before scaffolding.\nstring migrationName = /* from user */;\nvar existing = ((Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly)\n    serviceProvider.GetRequiredService<Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly>())\n    .FindMigrationId(migrationName);\nif (existing is not null)\n    throw new InvalidOperationException($\"A migration named '{migrationName}' already exists ({existing}).\");\n\nscaffolder.ScaffoldMigration(migrationName, rootNamespace, subNamespace);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["List existing migrations (`dotnet ef migrations list`) before adding a new one.","In CI, generate names with a timestamp/feature token to avoid collisions across branches.","When merging branches, reconcile any same-named migrations before continuing."],"tags":["ef-core","migrations","naming","design-time"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}