asp.net-mvc 在包管理器控制台中运行 enable-migrations 时出现错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13471444/
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
I get Error when i run enable-migrations in package manager console
提问by fizmhd
I am making a ASP.NET MVC project ...when i type enable-migrations i get the following eroors:
我正在制作一个 ASP.NET MVC 项目......当我输入 enable-migrations 时,我得到以下内容:
More than one context type was found in the assembly 'eManager.Web'.
To enable migrations for eManager.Web.Infrastructure.DepartmentDb, use Enable-Migrations -ContextTypeName eManager.Web.Infrastructure.DepartmentDb.
To enable migrations for eManager.Web.Models.UsersContext, use Enable-Migrations -ContextTypeName eManager.Web.Models.UsersContext.
回答by Pawel
The error message exactly states what the problem is and what needs to be done - including the command that needs to be issued. Apparently there is more than one context in your project (Web.Infrastructure.DepartmentDb and Web.Models.UsersContext) and migrations does not know for which of these migrations should be enabled. You need to point to the context type. As per the error message use:
错误消息准确说明了问题所在以及需要执行的操作 - 包括需要发出的命令。显然,您的项目中有多个上下文(Web.Infrastructure.DepartmentDb 和 Web.Models.UsersContext)并且迁移不知道应该为这些迁移中的哪一个启用。您需要指向上下文类型。根据错误消息使用:
Enable-Migrations -ContextTypeName eManager.Web.Infrastructure.DepartmentDb.
to enable migrations for eManager.Web.Infrastructure.DepartmentDb or
启用 eManager.Web.Infrastructure.DepartmentDb 的迁移或
Enable-Migrations -ContextTypeName eManager.Web.Models.UsersContext.
to enable migrations for eManager.Web.Models.UsersContext
为 eManager.Web.Models.UsersContext 启用迁移
回答by seguya
For those that may want to remain with a single context in the project. In this case, it will be the DepartmentDb context.
对于那些可能希望在项目中保持单一上下文的人。在这种情况下,它将是 DepartmentDb 上下文。
Move the below code into your DepartmentDb context:
将以下代码移动到 DepartmentDb 上下文中:
public DepartmentDb()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
Next: Get to your AccountModels.cs and delete/comment out the UsersContext class. You will get build errors - so replace the UsersContext references with DepartmentDb.
下一步:访问您的 AccountModels.cs 并删除/注释掉 UsersContext 类。你会得到构建错误——所以用 DepartmentDb 替换 UsersContext 引用。
Build again and it should succeed.
再次构建,它应该会成功。
Now go to the Package Manager Console and run PM> enable-migrations
现在转到包管理器控制台并运行 PM> enable-migrations
You should get "Code First Migrations enabled for project eManager.Web."
您应该获得“为项目 eManager.Web 启用代码优先迁移”。

