问题描述
我在搭建和从具有多个 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
但这甚至无法通过命令行解析器获得.我希望我所有的 DBContexts 和数据库内容都来自主 MVC 6 web 项目和它们自己的 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
的类库中.右键单击项目并选择Edit *.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
,您可以开始创建迁移.转到您的类库的文件夹.最简单的方法是右键单击项目并在文件资源管理器中打开文件夹
.然后,在 File Explorer
的地址栏中键入 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 migrations add 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 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!