EF 7 Migrations with multiple DBContexts

entity-framework-core

Question

I have a problem scaffolding and doing migrations from a class library with multiple DBContexts. I found a command line argument that looks like this for migrations:

dnx ef migration add -c Contexts.IndustryContext initial

But this doesn't even get by the command line parser. I want all my DBContexts and database stuff out of the main MVC 6 web project and in their own DLLs. Is this possible? What command line magic is required?

Accepted Answer

I was looking for an 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 a DbContext in them. 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: the version is the latest at the time of this post and will likely change in the future.

Next, I created a new Console App (.NET Core) called Migrations.Console and added it to my Solution. You will have to reference all of your DbContext class libraries in this project.

I installed Microsoft.EntityFrameworkCore and Microsoft.EntityFrameworkCore.Design Nuget packages.

In the Migrations.Console app, I created a DbContextFactory class for each Db context 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 match your project.

Now that each DbContextFactory is created, you can start to create the migrations. Go to the folder for your class library. The easiest way it to right click the project and Open Folder in File Explorer. Then, type cmd in the address bar of the File Explorer to open a command prompt in that folder.

Now use the following command to create the migration:

dotnet ef migrations add InitialCreate -c ApplicationDbContext --startup-project ../Migrations.Console/Migrations.Console.csproj

Note: Change ApplicationDbContext to match the name of the context you are working with. Also, if you called the console project by another name, you will need to change the path and name.

You should now see a Migrations folder in your class library.


Popular Answer

I haven't tried putting the data-layer in a separate project yet, but I do have multiple DbContexts in a single Web API project. It should work with separate projects too.

Using the latest syntax (v1.0.0), you create your migrations like this:

dotnet ef migrations add <migration-name> -o <output-directory> -c <context>

Where <context> is the full class-name of your DbContext -- like MyProject.Data.MyDbContext

I put my migrations for different contexts into different directories, but I don't think you have to. Just make sure they have different migration-name values.



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