在单独的程序集中启用带有上下文的迁移?

编程入门 行业动态 更新时间:2024-10-24 01:55:37
本文介绍了在单独的程序集中启用带有上下文的迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个项目要运行我的 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

更多推荐

在单独的程序集中启用带有上下文的迁移?

本文发布于:2023-05-26 16:23:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!