dotnet/efcore · error · OperationException
A migration name must be specified.
Error message
A migration name must be specified.
What it means
AddMigration / AddAndApplyMigration -> PrepareForMigration requires a non-empty migration name (line 438). The name becomes a file name and a class name, so an empty or whitespace string is rejected up front.
Source
Thrown at src/EFCore.Design/Design/Internal/MigrationsOperations.cs:440
var migrationsAssembly = scope.ServiceProvider.GetRequiredService<IMigrationsAssembly>();
var compiledAssembly = compiler.CompileMigration(migration, context.GetType());
migrationsAssembly.AddMigrations(compiledAssembly);
migrator.Migrate(migration.MigrationId);
_reporter.WriteInformation(DesignStrings.MigrationCreatedAndApplied(migration.MigrationId));
return files;
}
/// <summary>
/// Prepares common resources for migration operations.
/// </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);View on GitHub (pinned to dbf9771522)
Solutions
- Supply a meaningful PascalCase name, e.g. 'AddOrderStatus'.
- Validate/guard the name variable in your script before invoking the command.
- If calling the API, default to a generated name derived from the change.
Example fix
// before dotnet ef migrations add "" // after dotnet ef migrations add AddOrderStatus
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(migrationName)) throw new ArgumentException("A migration name is required."); Type guard
static bool IsValidMigrationName(string? n) => !string.IsNullOrWhiteSpace(n);
Prevention
- Default to a generated name when none is supplied.
- Validate CLI args in wrapper scripts.
When it happens
Trigger: Calling dotnet ef migrations add with no argument, or AddMigration(name: "") / AddMigration(name: null) via the API. string.IsNullOrWhiteSpace(name) is true.
Common situations: Scripting the CLI and forgetting to interpolate the name; invoking the API programmatically with a computed name that came back empty; tooling that prompts for a name and got an empty response.
Related errors
- The migration name '{name}' is not valid. Migration names ca
- The name you have chosen for the migration, '{name}', is the
- No DbContext named '{name}' was found.
- The wildcard '*' can only be used with commands that run for
- No DbContext was found in assembly '{assembly}'. Ensure that
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/ca44009556517e11.
Report an issue: GitHub.