dotnet/efcore · error · OperationException

The name you have chosen for the migration, '{name}', is the

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

The chosen migration name equals the DbContext class name, which would collide with the generated migration class (EF derives a class named <MigrationName> from the name). PrepareForMigration compares name to context.GetType().Name with Ordinal and rejects the match, suggesting 'InitialCreate' for the first migration.

Source

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

    /// </summary>
    public virtual IServiceProvider PrepareForMigration(string name, DbContext context)
    {
        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"));
        }
    }

View on GitHub (pinned to dbf9771522)

Solutions

  1. Pick a different name describing the change, e.g. 'Init' or 'InitialCreate' for the first migration.
  2. If scripting, append a suffix like '_Initial' to the context name.
  3. Follow the convention of verb-noun migration names (AddX, RemoveY).

Example fix

// before: context class is SalesDbContext
dotnet ef migrations add SalesDbContext
// after
dotnet ef migrations add InitialCreate
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Running 'dotnet ef migrations add <ContextName>' where <ContextName> is literally the DbContext subclass name (e.g. context is SalesDbContext and you pass 'SalesDbContext').

Common situations: Developer names the first migration after the context out of habit; templating scripts that interpolate the context name into the migration name.

Related errors


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