在单独的程序集中启用带有上下文的迁移? [英] Enable Migrations with Context in Separate Assembly?
问题描述
我有一个项目要运行我的 update-database
,但我的模型和上下文在一个单独的项目中.
I have one project that I want to run my update-database
against but I have my Models and Context in a separate project.
如果我运行 enable-migrations
我得到这个错误:在程序集MyProject"中找不到上下文类型.
If I run enable-migrations
I get this error:
No context type was found in the assembly 'MyProject'.
这大概是因为我的上下文在 MyProject.MVC 中.
This is presumably because my Context is in MyProject.MVC.
如果我针对 MyProject.MVC 运行 enable-migrations
,我必须添加一个应用程序配置文件.我不想这样做,因为我想在许多项目中使用这些代码.
If I run enable-migrations
against MyProject.MVC I have to add an app config file. I don't want to do that as I want to use the code across many projects.
那么我可以对 MyProject 运行 enable-migrations
并以某种方式告诉它在 MyProject.MVC 中查找上下文吗?
So can I run enable-migrations
against MyProject and somehow tell it to look in MyProject.MVC for the Context?
推荐答案
这只适用于 EF 6,但有一个 release 将 -ContextProjectName
参数添加到 -enable-migrations
命令.通过使用此命令,您可以执行以下操作:
This will only work in EF 6, but there was a release that added the -ContextProjectName
parameter to the -enable-migrations
command. By using this command you could do the following:
enable-migrations -ContextProjectName MyProject.MVC -StartUpProjectName MyProject.MVC
-ContextTypeName MyProject.MVC.MyContextFolder.MyContextName -ProjectName MyProject
这将使用 MyProject.MVC
中的上下文将迁移添加到您的 MyProject
项目.您需要确保带有迁移的项目具有对带有上下文的项目的引用,即 MyProject
引用 MyProject.MVC
This will add migrations to your MyProject
project using the context in the MyProject.MVC
.
You need to make sure that the project with the Migrations has a reference to the project with your Context, i.e., MyProject
references MyProject.MVC
这篇关于在单独的程序集中启用带有上下文的迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!