dotnet/efcore · error · OperationException

The name you have chosen for the migration

Error message

The name you have chosen for the migration, '{name}', is the same as the context class name. Please choose a different name for your migration. Might we suggest 'InitialCreate' for your first migration?

What it means

PrepareForMigration forbids a migration whose name equals the DbContext class's simple type name (ordinal, case-sensitive compare). EF generates a migration class derived from Migration named '<MigrationName>'; if that collides with the context class name the generated code would shadow/conflict with the context type, and the scaffolded namespace would be ambiguous. The message suggests 'InitialCreate' as a safe default.

Solutions

  1. Choose a migration name different from your DbContext class name (e.g. 'InitialCreate', 'AddProducts').
  2. Rename the DbContext if the migration name is intentional and the context name is the one you'd rather change.

Example fix

// DbContext: ShopDbContext
# before
dotnet ef migrations add ShopDbContext
# after
dotnet ef migrations add InitialCreate
Defensive patterns

Strategy: validation

Validate before calling

if (string.Equals(name, context.GetType().Name, StringComparison.Ordinal))
{
    throw new ArgumentException("Migration name must differ from the DbContext class name.");
}

Try / catch

try { operations.AddMigration(name, ...); }
catch (OperationException ex) when (ex.Message.Contains("same as the context class name"))
{ /* choose a different name like InitialCreate */ }

Prevention

When it happens

Trigger: Calling PrepareForMigration where name exactly equals context.GetType().Name (case-sensitive). For a context 'ShopDbContext', passing 'ShopDbContext' trips the guard.

Common situations: User names the first migration after the context by reflex; tooling auto-suggests the context name; the context was renamed and an old migration name now matches the new context name.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/2fba6a839052251c. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Design/Design/Internal/MigrationsOperations.cs:455

    {
        if (string.IsNullOrWhiteSpace(name))
        {
            throw new OperationException(DesignStrings.MigrationNameRequired);
        }

        var invalidPathChars = Path.GetInvalidFileNameChars();
        if (name.Any(c => invalidPathChars.Contains(c)))
        {
            throw new OperationException(
                DesignStrings.BadMigrationName(name, string.Join("','", invalidPathChars)));
        }

        var contextClassName = context.GetType().Name;
        if (string.Equals(name, contextClassName, StringComparison.Ordinal))
        {
            throw new OperationException(
                DesignStrings.ConflictingContextAndMigrationName(name));
        }

        var services = _servicesBuilder.Build(context);
        EnsureServices(services);

        return services;
    }

    private static void EnsureServices(IServiceProvider services)
    {
        var migrator = services.GetService<IMigrator>();
        if (migrator == null)
        {
            var databaseProvider = services.GetService<IDatabaseProvider>();
            throw new OperationException(DesignStrings.NonRelationalProvider(databaseProvider?.Name ?? "Unknown"));
        }
    }

    private void EnsureMigrationsAssembly(IServiceProvider services)

View on GitHub (pinned to 3a2006ef56)