启用迁移与上下文中单独的程序? [英] Enable Migrations with Context in Separate Assembly?
问题描述
我有,我想我的更新数据库
对运行一个项目,但我有我的模型和上下文在一个单独的项目。
I have one project that I want to run my update-database
against but I have my Models and Context in a separate project.
如果我跑启用-迁移
我得到这个错误:
没有上下文类型被发现的程序集MyProject的。
If I run enable-migrations
I get this error:
No context type was found in the assembly 'MyProject'.
这是presumably,因为我的背景是在MyProject.MVC。
This is presumably because my Context is in MyProject.MVC.
如果我跑启用-迁移
对MyProject.MVC我必须添加一个应用程序配置文件。我不想这样做,因为我想用code在许多项目中。
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的,不知怎么告诉它看在MyProject.MVC的背景下?
So can I run enable-migrations
against MyProject and somehow tell it to look in MyProject.MVC for the Context?
推荐答案
这将仅适用于EF 6,但有一个的 -ContextProjectName 参数添加到 -enable-迁移
命令>发布。通过使用此命令可以执行以下操作:
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屋!