簡體   English   中英

VS 2015 ASP.NET Web API(EF6)和Xamarin Enable-Migrations失敗

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

我正在開發一個項目,它將使用ASP.NET Web API作為數據服務,並使用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 Core不支持的TPT / TPH模型),所以EF版本是針對.NET 4.6.1的6.1.3。

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

更新

無法重現,但即使添加虛擬啟動項目也會出現問題。
Cross發布此處的問題,請投票並分享您的實驗。

它似乎抱怨Using StartUp project 'MyProject.App'但你已經用-StartupProjectName MyProject.App指定了啟動項目名稱

你能嘗試一下:

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

確保在啟動項目配置文件中您有一個有效的連接字符串(除非您在DbContext構造函數中指定連接字符串名稱,您的連接字符串應該被稱為ApplicationDbContext,就像您的DbContext,如果我沒記錯的話)


更新我低估了這個問題。 似乎可能不是您如何指定啟動項目,而是啟動項目本身。 我建議看看這個答案 正如我之前所說,請特別注意連接字符串存在於啟動項目中的web或app.config中,並且具有正確的名稱。

根據這個 (測試和工作),在aspnetcore + ef6項目中啟用遷移的唯一方法是將DbContext impl放在外部完整的.NET類庫中,並添加一個虛擬啟動項目。
糟透了但很有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM