EF 7 Migrations. Web project, DBContext and Migrations in different assemblies

asp.net-core asp.net-core-mvc entity-framework-core

Question

I have three projects in my solution.

BaseApp.Data - which holds my DBContext.

BaseApp.Data.ProjectMigration - which holds all migration files (depends on BaseApp.Data)

BaseApp.Web - web application with Startup class.

I added my first migration like this:

dnx ef migrations add Initial -p BaseApp.Data.ProjectMigration

After this command the Migrations folder appears with all corresponding files in BaseApp.Data.ProjectMigration. So everything is ok so far.

Than I try to apply migrations:

dnx ef database update

After this command the database is created, but no migrations applied. I also tried

dnx ef database update -p BaseApp.Data.ProjectMigration

with same result. Also next command returns "No migrations were found".

dnx ef migrations list -p BaseApp.Data.ProjectMigration

Also if I add migrations to BaseApp.Data then everything works fine. The next commands work as expected:

dnx ef migrations add Initial -p BaseApp.Data

dnx ef database update

So is it possible to hold migration files not in DBContext project?

Accepted Answer

You'll also need to set your migrations assembly.

options.UseSqlServer(connectionString)
    .MigrationsAssembly("BaseApp.Data.ProjectMigration");


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why