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 haven't added any, add one to the DbContext project then move it.
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>..\MyStarupProject\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 migraitons add NewMigration --project MyApp.Migrations