I was looking for the answer to this question and wanted to provide my solution for ef core 2.0.
Microsoft.EntityFrameworkCore.Tools.DotNet needs to be added to each of your class libraries that have DbContext . Right-click the project and select Edit *.csproj . Then add the following:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" /> </ItemGroup>
Note: this version is the latest at the time of publication and is likely to change in the future.
Then I created a new console application (.NET Core) called Migrations.Console and added it to my solution. You will need to reference all of your DbContext class DbContext in this project.
I installed the Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design Nuget packages.
In the Migrations.Console application, I created a DbContextFactory class for every Db context that I have.
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> { public ApplicationDbContext CreateDbContext(string[] args) { var builder = new DbContextOptionsBuilder<ApplicationDbContext>(); builder.UseSqlServer("Server=(local);Database=DATABASENAME;Trusted_Connection=True;MultipleActiveResultSets=true"); return new ApplicationDbContext(builder.Options); } }
Note. Make sure you update the context and connection string to suit your project.
Now that every DbContextFactory is created, you can start creating migrations. Browse to the folder for your class library. The easiest way to right-click on a project and Open Folder in File Explorer . Then type cmd in the address bar of File Explorer to open a command prompt in this folder.
Now to create the transfer, now use the following command:
dotnet ef migrations add InitialCreate -c ApplicationDbContext --startup-project ../Migrations.Console/Migrations.Console.csproj
Note. Change the ApplicationDbContext to match the name of the context you are working with. In addition, if you called the console project with a different name, you will need to change the path and name.
You should now see the Migrations folder in your class library.
Todd skelton
source share