本文介绍了带有多个DBContext的EF 7迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在脚手架和从具有多个DBContext的类库中进行迁移时遇到问题。我发现用于迁移的命令行参数如下:

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

但是,命令行解析器甚至无法做到这一点。我希望所有MDB 6 Web项目中的所有DBContexts和数据库内容都不在其自己的DLL中。这可能吗?

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?

推荐答案

我一直在寻找这个问题的答案,并想为我的ef core提供解决方案。 2.0。

I was looking for an answer to this question and wanted to provide my solution for ef core 2.0.

Microsoft.EntityFrameworkCore.Tools.DotNet 需要添加到每个具有其中的 DbContext 。右键单击该项目,然后选择编辑* .csproj 。然后,添加以下内容:

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.

接下来,我创建了一个名为Migrations.Console的新控制台应用程序(.NET Core),并将其添加到我的解决方案中。您将必须引用该项目中的所有 DbContext 类库。

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.

我安装了 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore.Design Nuget包。

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

Migrations.Console 应用程序,我为我拥有的每个Db上下文创建了一个DbContextFactory类。

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.

现在,每个 DbContextFactory 已创建,您可以开始创建迁移。转到您的课程库的文件夹。右键单击项目并在文件资源管理器中打开文件夹的最简单方法。然后,在文件资源管理器的地址栏中键入 cmd ,以在该文件夹中打开命令提示符。

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迁移添加InitialCreate -c ApplicationDbContext --startup-project ../ Migrations.Console / Migrations.Console.csproj

注意:更改 ApplicationDbContext 以匹配您正在使用的上下文的名称。另外,如果您使用其他名称调用控制台项目,则需要更改路径和名称。

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.

您现在应该看到 Migrations 类库中的文件夹。

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

这篇关于带有多个DBContext的EF 7迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:16