dotnet/efcore · error · OperationException

You cannot add a migration with the name 'Migration'.

Error message

You cannot add a migration with the name 'Migration'.

What it means

EF Core generates every migration as a class that derives from the `Migration` base class. Naming a migration "Migration" would produce a class literally called `Migration` deriving from `Migration` — a circular base-class dependency — so the scaffolder rejects the name up front (the resource key is `CircularBaseClassDependency`). The check is case-insensitive (`OrdinalIgnoreCase`), so "migration", "MIGRATION", etc. are all blocked.

Source

Thrown at src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs:75

    ///     namespace generation, just use sub-namespace as is.
    /// </param>
    /// <param name="subNamespace">
    ///     The migration's sub-namespace. Note: the root-namespace and
    ///     the sub-namespace should not both be empty.
    /// </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 =

View on GitHub (pinned to dbf9771522)

Solutions

  1. Re-run the command with a descriptive, unique migration name (e.g. `InitialCreate`, `AddUserTable`).
  2. If a script generates the name, ensure it never yields the literal "migration".

Example fix

// before
dotnet ef migrations add Migration
// after
dotnet ef migrations add InitialCreate
Defensive patterns

Strategy: validation

Validate before calling

// Validate migration name before scaffolding.
static string ValidateMigrationName(string name)
{
    if (string.IsNullOrWhiteSpace(name))
        throw new ArgumentException("Migration name is required.", nameof(name));
    if (string.Equals(name, "migration", StringComparison.OrdinalIgnoreCase))
        throw new ArgumentException("A migration cannot be named 'Migration' (circular base class).");
    if (!System.Text.RegularExpressions.Regex.IsMatch(name, @"^[A-Za-z_][A-Za-z0-9_]*$"))
        throw new ArgumentException("Migration name must be a valid C# identifier.");
    return name;
}

// usage
var name = ValidateMigrationName(args[0]);
scaffolder.ScaffoldMigration(name, rootNamespace, subNamespace);

Prevention

When it happens

Trigger: Running `dotnet ef migrations add Migration`, `Add-Migration Migration` (PMC), or programmatically calling `MigrationsScaffolder.ScaffoldMigration("Migration", rootNamespace, subNamespace)` with any casing of the literal string "migration".

Common situations: Using a placeholder/generic name while experimenting, copy-pasting a command template whose name argument defaulted to the word "migration", or an automation script that passes a fixed string literal.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/5fb65d4c7a4cfe08. Report an issue: GitHub.