ASP.NET 核心 EF 添加迁移命令不起作用

ASP.NET Core EF Add-Migration command not working

按照此 Microsoft Tutorial 当我 运行 从同一教程创建的 VS2015 项目中的 PM> Add-Migration MyFirstMigration 命令时,我收到以下无法解决的错误:

More than one DbContext was found. Specify which one to use.
Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.

注意事项

  1. 我使用的是上述教程,但我使用的是 Individual User Account 身份验证而不是教程中使用的 No Authentication
  2. 我有最新版本的 ASP.NeT Core 1.0 and VS2015-Update 3 on windows 8.1
  3. 这是一个新创建的项目。没有手动安装其他 DbContext

运行 以下命令(从 this article) and a response from @Maverik (from Whosebug 获得)和上面@doctor 的建议帮助我解决了这个问题。谢谢大家的帮助:

PM> Add-Migration MyFirstMigration -Context BloggingContext

那是因为您的解决方案中有两个 DbContext。首先是在您创建项目 (ApplicationDbContext) 时默认创建的,其次是您的 EF DbContext。 错误消息中描述了解决方案,只需指定您的 EF DbContext

如果有多个 DbContext,该错误清楚地解释了要提及 --context 和 db 上下文名称。因此,请尝试提及您的 DbContext 名称。

dotnet ef migrations add Initial --context SampleDbContext

希望对您有所帮助。

如果您只需要更新现有的身份架构,请尝试:

update-database -Context ApplicationDbContext

ApplicationDbContext = 您的身份上下文

Add-Migration MyFirstMigration -Context DbContextName

它在我的项目中确实有效。

使用下面的命令:

PM> Add-Migration MyFirstMigration -Context YourDbContext

PM> update-database -Context YourDbContext

[--上下文]

The DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will find the context class. If there are multiple context classes, this option is required.

Add-Migration MyMigration -context DataContextName