17

我正在開發將使用的ASP.NET Web API作爲數據服務的項目,以及Xamarin便攜式應用的客戶端。VS 2015年的ASP.NET Web API(EF6)Xamarin啓用的遷移失敗

我想能夠在Web應用程序遷移,但我得到了以下錯誤:

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\6.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\6.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(我一定到EF核心還不支持TPT/TPH模型),所以EF版本是6.1.3針對.NET 4.6.1。

我在VS 2015年社區更新3版本14.0.25431.01。

更新

無法重現,但添加一個虛擬的啓動項目時,這個問題甚至發生。
Cross發佈問題here,請投票並分享您的實驗。

+0

您的啓動項目是否包含您用來連接數據庫的app.config或web.config?您也可以使用包含dbcontext的項目作爲啓動項目。 – jomsk1e

+0

它使用appsettings.json來存儲連接字符串。但我嘗試將連接字符串添加到app.config,但它沒有幫助。 – Shimmy

回答

1

根據this(經過測試和工作),在aspnetcore + ef6項目中啓用遷移的唯一方法是在外部完整的.NET類庫中加入DbContext impl,並添加一個虛擬啓動項目。
吮吸,但工程。

1

看來它在抱怨Using StartUp project 'MyProject.App'但你已經與-StartupProjectName MyProject.App指定的啓動項目名稱

你可以嘗試只:

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

確保在您啓動時,你有項目配置文件一個有效的連接字符串(除非您指定在的DbContext構造一個連接字符串名稱,連接字符串應該叫ApplicationDbContext,喜歡你的DbContext,如果我沒有記錯)


UPDATE 我低估了這個問題。看起來可能不是你如何指定啓動項目,而是啓動項目本身。我建議看看this answer。正如我之前所說的,要特別注意連接字符串存在於啓動項目的web或app.config中,並且名稱正確。

+0

我試過了,[this](http://pastebin.com/eAKYA7vN)是輸出。 – Shimmy

+1

對更新的評論:我驗證了所有的連接字符串都設置好了,並且啓動項目(項目本身)也設置了web.config。我相信唯一的選擇是添加一個虛擬控制檯應用程序並將其設置爲啓動。 – Shimmy

+0

爲此努力獲得25獎金,無論如何答案不起作用。我已在EF6回購上發佈了一個[問題](https://github.com/aspnet/EntityFramework6/issues/317),請評論。 – Shimmy