dotnet/efcore · error · InvalidOperationException

The migration ' ' was not found.

Error message

The migration '{migrationName}' was not found.

What it means

MigrationsAssemblyExtensions.GetMigrationId asks the registered IMigrationsAssembly to resolve a migration by name or id and got null. EF needs the migration's full id (e.g. '20240101000000_Init') to know which file/class to instantiate. Used by the Migrator when a target migration name is supplied on the command line or via Migrate(target).

Solutions

  1. Run 'dotnet ef migrations list' (or query db.Database.GetMigrations()) to see the exact available names and ids.
  2. Pass the correct name (case-sensitive suffix or full timestamped id).
  3. Verify options.MigrationsAssembly points at the assembly that actually contains the migration classes.
  4. If the migration was removed, target a still-existing migration or '0' to revert all.

Example fix

// before
await db.Database.MigrateAsync("Inital"); // typo

// after
await db.Database.MigrateAsync("Initial");
// or full id
await db.Database.MigrateAsync("20240101000000_Initial");
Defensive patterns

Strategy: validation

Validate before calling

// Verify a migration name resolves before passing it to Migrate
static string ResolveMigration(DbContext db, string nameOrId)
{
    var assembly = db.GetService<IMigrationsAssembly>();
    var id = assembly.FindMigrationId(nameOrId);
    return id ?? throw new InvalidOperationException(
        $"Migration '{nameOrId}' not found. Available: " +
        string.Join(", ", assembly.Migrations.Keys));
}

Type guard

static bool MigrationExists(DbContext db, string name)
    => db.GetService<IMigrationsAssembly>().FindMigrationId(name) is not null;

Prevention

When it happens

Trigger: Database.Migrate("WrongName"), 'dotnet ef database update WrongName', or any code passing a migration name that does not match any registered migration id or suffix in the configured migrations assembly.

Common situations: Typo in the migration name; migration lives in a different assembly not configured via options.UseSqlServer(conn, sql => sql.MigrationsAssembly(typeof(...).Assembly.FullName)); migration was deleted/renamed but the script still references the old name; target name includes the timestamp prefix that does not match.

Related errors


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

Appendix: source

Thrown at src/EFCore.Relational/Migrations/MigrationsAssemblyExtensions.cs:35

    /// </summary>
    /// <remarks>
    ///     <para>
    ///         An exception is thrown if the migration was not found--use
    ///         <see cref="IMigrationsAssembly.FindMigrationId" /> if the migration may not exist.
    ///     </para>
    ///     <para>
    ///         See <see href="https://aka.ms/efcore-docs-migrations">Database migrations</see> for more information and examples.
    ///     </para>
    /// </remarks>
    /// <param name="assembly">The assembly.</param>
    /// <param name="nameOrId">The name or identifier to lookup.</param>
    /// <returns>The identifier of the migration.</returns>
    public static string GetMigrationId(this IMigrationsAssembly assembly, string nameOrId)
    {
        Check.NotEmpty(nameOrId);

        var id = assembly.FindMigrationId(nameOrId);
        return id ?? throw new InvalidOperationException(RelationalStrings.MigrationNotFound(nameOrId));
    }
}

View on GitHub (pinned to 3a2006ef56)