VS 2015 ASP.NET Web API (EF6) & Xamarin 启用迁移失败

VS 2015 ASP.NET Web API (EF6) & Xamarin Enable-Migrations fails

我正在开发一个项目,该项目将使用 ASP.NET Web API 作为数据服务,并使用 Xamarin 便携式应用程序作为客户端。

我尝试在网络应用程序中启用迁移,但出现以下错误:

Enable-Migrations -enableautomaticmigrations -ContextTypeName MyProject.Models.ApplicationDbContext -ProjectName MyProject -StartupProjectName MyProject.App -Verbose
Using StartUp project 'MyProject.App'.
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At C:\Users\weitz\.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:718 char:5
+     $domain.SetData('project', $project)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SerializationException
 
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At C:\Users\weitz\.nuget\packages\EntityFramework.1.3\tools\EntityFramework.psm1:719 char:5
+     $domain.SetData('contextProject', $contextProject)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SerializationException
 
System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue[T](Project project, String propertyName)
   at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.
**PM>**

如您所见,我已经尝试明确指定启动项目,但看起来 enable-migrations 命令对此并不满意。

这是我刚刚创建的一个使用完整 .NET 的项目(我绑定到 TPT/TPH EF Core 尚不支持的模型),因此 EF 版本是 6.1.3,目标是 .NET 4.6 .1.

我正在使用 VS Community 2015 Update 3 版本 14.0.25431.01。

更新

无法重现,但即使添加虚拟启动项目也会出现此问题。
交叉发布问题 here,请投票并分享您的实验。

它似乎在抱怨 Using StartUp project 'MyProject.App' 但你已经用 -StartupProjectName MyProject.App

指定了启动项目名称

只能试试吗:

Enable-Migrations -enableautomaticmigrations -ContextTypeName MyProject.Models.ApplicationDbContext -ProjectName MyProject -StartupProjectName MyProject.App -Verbose

确保在您的启动项目配置文件中有一个有效的连接字符串(除非您在 DbContext 构造函数中指定了一个连接字符串名称,如果我没记错的话,您的连接字符串应该被称为 ApplicationDbContext,就像您的 DbContext )


更新 我低估了这个问题。似乎这可能不是您指定启动项目的方式,而是启动项目本身。我建议查看 。特别注意,正如我之前所说,连接字符串存在于网络中或启动项目中 app.config 并且具有正确的名称。

好吧,根据 this(经过测试和工作),在 aspnetcore+ef6 项目中启用迁移的唯一方法是在外部完整 .NET [=15] 中使用 DbContext impl =] 库,加上一个虚拟启动项目。
糟糕但有效。