Using a Separate Project
You may want to store your migrations in a different assembly than the one containing your
DbContext. You can also
use this strategy to maintain multiple sets of migrations, for example, one for development and another for
To do this...
Create a new class library.
Add a reference to your DbContext assembly.
Move the migrations and model snapshot files to the class library.
If you have no existing migrations, generate one in the project containing the DbContext then move it. This is important because if the migrations assembly does not contain an existing migration, the Add-Migration command will be unable to find the DbContext.
Configure the migrations assembly:
options.UseSqlServer( connectionString, x => x.MigrationsAssembly("MyApp.Migrations"));
Add a reference to your migrations assembly from the startup assembly.
If this causes a circular dependency, update the output path of the class library:
<PropertyGroup> <OutputPath>..\MyStartupProject\bin\$(Configuration)\</OutputPath> </PropertyGroup>
If you did everything correctly, you should be able to add new migrations to the project.
Add-Migration NewMigration -Project MyApp.Migrations
dotnet ef migrations add NewMigration --project MyApp.Migrations
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.