Skip to main content

Fixed: "Your target project 'Dewiride.Inventory.Api' doesn't match your migrations assembly 'Dewiride.Inventory.Data'" in EF Core

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Your target project 'Dewiride.Inventory.Api' doesn't match your migrations assembly 'Dewiride.Inventory.Data'. Either change your target project or change your migrations assembly. dotnet ef built the solution, then refused to write the migration, because it would have put the files in a project where EF Core would never look for them. The message offers two fixes; one of them moves your migrations to the web project, and it's usually not the one you want.

What I Was Doing​

In a .NET 10 solution, the web API references a class library that holds the EF Core DbContext. The API registers the DbContext in Program.cs, and it has the Microsoft.EntityFrameworkCore.Design package that dotnet ef needs. So I ran the command from the API's folder, the way single-project tutorials do:

Terminal (in src/Dewiride.Inventory.Api)
dotnet ef migrations add CreateCatalogTables

What I Expected​

A new migration.

What I Got​

Output
Build started...
Build succeeded.
Your target project 'Dewiride.Inventory.Api' doesn't match your migrations assembly 'Dewiride.Inventory.Data'. 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("Dewiride.Inventory.Api")). 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.

A terminal where dotnet ef migrations add CreateCatalogTables, run in the API folder, builds and then reports that the target project Dewiride.Inventory.Api doesn't match the migrations assembly Dewiride.Inventory.Data, with the two suggested fixes.

What I Checked​

The two names in the message.

  • The target project, Dewiride.Inventory.Api, is where dotnet ef would write the migration files. EF Core's tools reference says the target project is "where the commands add or remove files. By default, the project in the current directory is the target project." I was in the API's folder.
  • The migrations assembly, Dewiride.Inventory.Data, is where EF Core looks for migrations at run time. The message gives the rule: "By default, the migrations assembly is the assembly containing the DbContext." The DbContext is in the class library.

Whether every command fails. From the same folder, a command that only reads migrations worked:

Terminal (in src/Dewiride.Inventory.Api)
dotnet ef migrations list --no-connect
Output (last lines)
20261103044948_StoreMovementTypeAsString
20261103045135_AddMovementHistoryIndex
Pending status not shown. Unable to determine which migrations have been applied. This can happen when your project uses a version of Entity Framework Core lower than 5.0.0 or when an error occurs while accessing the database.

(I ran this later, once the solution had migrations; --no-connect lists them without asking the database.) So the API is a fine startup project: the tools can build it, run its startup code and find the migrations in the library. Only the target, where new files are written, was wrong.

Why It Happens​

Run from the API's folder with no options, the API was the target project, while the migrations assembly, where EF Core looks for migrations at run time, is the class library that holds the DbContext. Writing the files into the API while EF Core reads migrations from the library would create migrations that nothing ever applies, so the tools refuse.

The Fix​

The message offers two fixes:

  1. "Change your migrations assembly" with MigrationsAssembly("Dewiride.Inventory.Api"): the migrations would live in the web project.
  2. "Change your target project to the migrations project": write the files into the class library, next to the DbContext.

The second keeps the data layer in one project: the DbContext, its entities, its configuration and its migrations, so any other app that uses the library gets the same migrations. Name the library as the target and the API as the startup project, from the solution folder:

Terminal
dotnet ef migrations add CreateCatalogTables --project src/Dewiride.Inventory.Data --startup-project src/Dewiride.Inventory.Api

The message's "executing "dotnet ef" from the directory containing the migrations project" is the same fix from another folder: in src/Dewiride.Inventory.Data, the target is found by default, and --startup-project ../Dewiride.Inventory.Api names the API.

After the Fix​

Output
Build started...
Build succeeded.
Done. To undo this action, use 'ef migrations remove'

A terminal where dotnet ef migrations add CreateCatalogTables with --project src/Dewiride.Inventory.Data and --startup-project src/Dewiride.Inventory.Api builds and prints Done.

The migration went into src/Dewiride.Inventory.Data/Migrations/, inside the migrations assembly, where EF Core finds it.

When MigrationsAssembly Is the Right Choice​

Moving migrations out of the DbContext's assembly is a supported design, described in Using a Separate Migrations Project: "You can store migrations in a different project from the one containing your DbContext." Teams do it, for example, to keep migrations for different database providers apart. Then you configure MigrationsAssembly on purpose, in the provider's options, and point --project at that migrations project. Don't do it only because the error suggested it first.

Common Mistakes​

MistakeWhat happensDo this instead
Accepting MigrationsAssembly("…Api") to make the error go awayMigrations end up in the web project, away from the DbContextName the class library with --project
Copying the message's UseSqlServer example into a MySQL appIt's only an example; the provider method here is UseMySQLConfigure MigrationsAssembly in your own provider's options, if at all
Running from the class library's folder with no optionsThe next error: the library becomes the startup project without the Design packageAdd --startup-project

Troubleshooting​

SymptomLikely causeFix
Your target project … doesn't match your migrations assembly …The command ran in the app's folder, so the app is the target projectPass --project with the project that holds the DbContext
migrations list works in the same folder, migrations add doesn'tListing needs only a startup project; adding needs the right targetName the target with --project
Your migrations live in a separate project by designEF Core still looks for them in the DbContext's assemblyConfigure MigrationsAssembly in your provider's options

FAQ​

Why did the same command work in a tutorial with one project?​

With one project, the DbContext, the migrations and the startup code are all in it, so the target project and the migrations assembly are the same. The error only appears when they're in different projects.

Do database update and migrations remove need the same options?​

Use the same two options for every dotnet ef command in this solution. It's simpler than remembering which commands care about the target project.

Conclusion​

"Your target project … doesn't match your migrations assembly …" means dotnet ef would write migrations into one project while EF Core reads them from another, by default the one with the DbContext. In a solution with the DbContext in a class library, make the library the target with --project, keep the web app as the --startup-project, and leave MigrationsAssembly for when you really want migrations in a separate project.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.