dotnet/efcore · error · OperationException
The name '{migrationName}' is used by an existing migration.
Error message
The name '{migrationName}' is used by an existing migration. What it means
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.
Source
Thrown at src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs:80
/// </param>
/// <param name="language">The project's language.</param>
/// <param name="dryRun">If <see langword="true" />, then nothing is actually written to disk.</param>
/// <returns>The scaffolded migration.</returns>
public virtual ScaffoldedMigration ScaffoldMigration(
string migrationName,
string? rootNamespace,
string? subNamespace = null,
string? language = null,
bool dryRun = false)
{
if (string.Equals(migrationName, "migration", StringComparison.OrdinalIgnoreCase))
{
throw new OperationException(DesignStrings.CircularBaseClassDependency);
}
if (Dependencies.MigrationsAssembly.FindMigrationId(migrationName) != null)
{
throw new OperationException(DesignStrings.DuplicateMigrationName(migrationName));
}
var overrideNamespace = rootNamespace == null;
var subNamespaceDefaulted = false;
if (string.IsNullOrEmpty(subNamespace) && !overrideNamespace)
{
subNamespaceDefaulted = true;
subNamespace = "Migrations";
}
var (key, typeInfo) = Dependencies.MigrationsAssembly.Migrations.LastOrDefault();
var migrationNamespace =
(!string.IsNullOrEmpty(rootNamespace)
&& !string.IsNullOrEmpty(subNamespace))
? rootNamespace + "." + subNamespace
: !string.IsNullOrEmpty(rootNamespace)
? rootNamespaceView on GitHub (pinned to dbf9771522)
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.
Example fix
// before dotnet ef migrations add InitialCreate // already exists // after dotnet ef migrations add AddUsersTable
Defensive patterns
Strategy: validation
Validate before calling
// Check the migrations assembly for an existing name before scaffolding.
string migrationName = /* from user */;
var existing = ((Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly)
serviceProvider.GetRequiredService<Microsoft.EntityFrameworkCore.Migrations.IMigrationsAssembly>())
.FindMigrationId(migrationName);
if (existing is not null)
throw new InvalidOperationException($"A migration named '{migrationName}' already exists ({existing}).");
scaffolder.ScaffoldMigration(migrationName, rootNamespace, subNamespace); Prevention
- 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.
When it happens
Trigger: `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`.
Common situations: 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.
Related errors
- You cannot add a migration with the name 'Migration'.
- No ModelSnapshot was found.
- The migration '{name}' has already been applied to the datab
- No DbContext named '{name}' was found.
- More than one DbContext named '{name}' was found. Specify wh
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/55ba42d08df01561.
Report an issue: GitHub.