dotnet/efcore · error · OperationException
The migration name '{name}' is not valid. Migration names ca
Error message
The migration name '{name}' is not valid. Migration names cannot contain any of the following characters: '{characters}'. What it means
The migration name contains characters that are invalid in file names (Path.GetInvalidFileNameChars), because the name is used verbatim as the generated file's base name. PrepareForMigration rejects it before scaffolding so it never writes an unusable file.
Source
Thrown at src/EFCore.Design/Design/Internal/MigrationsOperations.cs:446
_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);
return services;
}
private static void EnsureServices(IServiceProvider services)
{View on GitHub (pinned to dbf9771522)
Solutions
- Use a PascalCase identifier with no punctuation, e.g. 'AddOrderStatus'.
- Strip or replace invalid characters before invoking the command.
- Use --output-dir for folder placement instead of encoding paths in the name.
Example fix
// before dotnet ef migrations add "Add Order/Status" // after dotnet ef migrations add AddOrderStatus --output-dir Migrations/Sales
Defensive patterns
Strategy: validation
Validate before calling
var invalid = Path.GetInvalidFileNameChars(); if (migrationName.Any(c => invalid.Contains(c))) throw new ArgumentException($"Migration name contains invalid characters.");
Type guard
static bool IsValidMigrationName(string n) => !n.Any(Path.GetInvalidFileNameChars().Contains);
Prevention
- Stick to PascalCase identifiers for migration names.
- Use --output-dir for folder structure, not the name.
When it happens
Trigger: Any name containing a character in Path.GetInvalidFileNameChars() for the OS -- on Windows that includes < > : \ / | ? * and control chars. e.g. 'Add(Order)' fails on '(' if the OS disallows it; most commonly '<', '>', ':', '\', '/', '*', '?', '"', '|'.
Common situations: Passing a sentence with punctuation; including slashes/path separators thinking they create subfolders; copy-pasting a name with parentheses or colons.
Related errors
- The name you have chosen for the migration, '{name}', is the
- A migration name must be specified.
- No DbContext named '{name}' was found.
- More than one DbContext named '{name}' was found. Specify wh
- 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/1c6b059e38b2d9e9.
Report an issue: GitHub.