启用迁移与上下文中单独的程序? [英] Enable Migrations with Context in Separate Assembly?

查看:160
本文介绍了启用迁移与上下文中单独的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想我的更新数据库对运行一个项目,但我有我的模型和上下文在一个单独的项目。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆