dotnet/efcore · error · OperationException

Your target project '{assembly}' doesn't match your migratio

Error message

Your target project '{assembly}' doesn't match your migrations assembly '{migrationsAssembly}'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("{assembly}")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.

What it means

EnsureMigrationsAssembly detected that the assembly EF is running commands from (_assembly, i.e. the project containing the migrations files on disk) does not match the configured MigrationsAssembly on the DbContext options. This guard prevents writing migration files into one project while the runtime expects them from another, which would silently break migration discovery.

Source

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

        }
    }

    private void EnsureMigrationsAssembly(IServiceProvider services)
    {
        var assemblyName = _assembly.GetName();
        var options = services.GetRequiredService<IDbContextOptions>();
        var contextType = services.GetRequiredService<ICurrentDbContext>().Context.GetType();
        var optionsExtension = RelationalOptionsExtension.Extract(options);
        if (optionsExtension.MigrationsAssemblyObject == null
            || optionsExtension.MigrationsAssemblyObject != _assembly)
        {
            var migrationsAssemblyName = optionsExtension.MigrationsAssembly
                ?? optionsExtension.MigrationsAssemblyObject?.GetName().Name
                ?? contextType.Assembly.GetName().Name;
            if (assemblyName.Name != migrationsAssemblyName
                && assemblyName.FullName != migrationsAssemblyName)
            {
                throw new OperationException(
                    DesignStrings.MigrationsAssemblyMismatch(assemblyName.Name, migrationsAssemblyName));
            }
        }
    }
}

View on GitHub (pinned to dbf9771522)

Solutions

  1. Run dotnet ef from / point --project at the migrations assembly: dotnet ef migrations add X -p Data -s Web.
  2. Or change the MigrationsAssembly in OnConfiguring to match the project you run from.
  3. Keep the configuration and the CLI project consistent: the value of MigrationsAssembly must equal the assembly named by --project.

Example fix

// before: ran from Web project, migrations configured for Data
// Web/OnConfiguring:
options.UseSqlServer(cs, b => b.MigrationsAssembly("Data"));
// after: run from the right project
dotnet ef migrations add Init -p ../Data -s .
Defensive patterns

Strategy: validation

Validate before calling

var configured = RelationalOptionsExtension.Extract(contextOptions).MigrationsAssembly;
if (!string.Equals(configured, targetAssembly.GetName().Name, StringComparison.Ordinal)) throw new InvalidOperationException("MigrationsAssembly mismatch; align --project with the configured value.");

Prevention

When it happens

Trigger: options.UseSqlServer(..., b => b.MigrationsAssembly("DataProject")) but you ran 'dotnet ef' from WebProject whose assembly name is 'WebProject'. At line 479-489, _assembly != MigrationsAssemblyObject and assemblyName.Name != migrationsAssemblyName.

Common situations: Multi-project solution where migrations live in a dedicated Data assembly but the entry project is different; dev ran dotnet ef from the wrong directory; migrations assembly configured without the matching --project.

Related errors


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