C# 无法为类库中的实体框架启用迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9527366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot enable migrations for Entity Framework in class library
提问by Chev
I just got on board with EF 5 and am using their code-first migrations tool but I seem to get an error when I try to enable migrations.
我刚刚加入 EF 5 并正在使用他们的代码优先迁移工具,但是当我尝试启用迁移时似乎出现错误。
I type Enable-Migrationsinto the package manager console and then it says
我输入Enable-Migrations包管理器控制台,然后它说
No classes deriving from DbContext found in the current project.
Edit the generated Configuration class to specify the context to enable migrations for.
Code First Migrations enabled for project MyApp.MvcUI.
在当前项目中找不到从 DbContext 派生的类。
编辑生成的 Configuration 类以指定启用迁移的上下文。
为项目 MyApp.MvcUI 启用代码优先迁移。
It then creates a Migrations folder and a Configuration class in my MvcUI project. Thing is, my DbContext lives in a class library project called MyApp.Domain. It should be doing all that in that project and should have no problem finding my DbContext.
然后在我的 MvcUI 项目中创建一个 Migrations 文件夹和一个 Configuration 类。事实是,我的 DbContext 存在于一个名为 MyApp.Domain 的类库项目中。它应该在那个项目中完成所有这些,并且找到我的 DbContext 应该没有问题。
采纳答案by Chev
Oh wow, nevermind. I'm dumb.
哦,哇,没关系。我很笨
In the Nuget package manager console there is a dropdown menu at the top labeled "Default Project:". Make sure you set that to the project you want to run the command against.
在 Nuget 包管理器控制台的顶部有一个下拉菜单,标有“默认项目:”。确保将其设置为要对其运行命令的项目。
Hopefully this helps someone else avoid my embarrassing mistake.
希望这可以帮助其他人避免我的尴尬错误。
回答by Jimmy
There are actually 3 ways to make Nuget commands run in a specific project:
实际上有 3 种方法可以让 Nuget 命令在特定项目中运行:
- [Package Manager Console] Set the active project in the dropdown at the top of the console toolwindow
- [Package Manager Console] Look for a parameter to specify the project. For some cmdlets I've seen -ProjectName and some use -Project
- [Solution Explorer] Right-click the project you want, and use the graphical package manager window (Manage NuGet Packages...).
- [Package Manager Console] 在控制台工具窗口顶部的下拉列表中设置活动项目
- [包管理器控制台] 查找参数以指定项目。对于我见过的一些 cmdlet -ProjectName 和一些使用 -Project
- [解决方案资源管理器] 右键单击所需的项目,并使用图形包管理器窗口(Manage NuGet Packages...)。

