1

今天早上我安裝了Visual Studio 2015社區版。之後,我創建了一個新的ASP.NET Web應用程序,並在「ASP.NET 5預覽模板」部分中選擇了「Web應用程序」模板。爲什麼啓用遷移失敗?

創建項目後,我加入了第二個項目,其中應該包含我所有的實體(EF代碼第一次),業務對象,公用事業及其他核心員工的溶液(類庫)。

的類庫使用的.NET Framework 4.5.2和引用的EntityFramework 6.1.3。我還添加了ASP.NET Identity 2.2.1(包括EntityFramework提供程序)。

在App.config中我有這樣的連接字符串:

<connectionStrings> 
    <add name="ApplicationDbContext" connectionString="Server=(localdb)\projectsv12;Database=myDB;Integrated Security=True" providerName="System.Data.SqlClient" /> 
</connectionStrings> 

這裏是相應的數據庫上下文類:

public class ApplicationDbContext : IdentityDbContext<User> { 

    public ApplicationDbContext() : base("name=ApplicationDbContext") { } 

    public static ApplicationDbContext Create() { 
    return new ApplicationDbContext(); 
    } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { 
    base.OnModelCreating(modelBuilder); 
    } 

    public System.Data.Entity.DbSet<Branch> Branches { get; set; } 
    public System.Data.Entity.DbSet<Dimension> Dimensions { get; set; } 
} 

正如你所看到的連接字符串的名稱是相同的App.config和DbContext構造函數。我還設置了兩個POCO實體並將它們添加到DbContext(分支和維度)中。

一切都應該準備「啓用的遷移」,然後「更新數據庫」正如我多次與Visual Studio做到了2013年。

然而,在Visual Studio 2015年,我收到一條錯誤信息。這是我做的:

  1. 我拉起包管理器控制檯。
  2. 我在「Default project」下拉列表中選擇了我的Class Library項目。
  3. 我進入了「啓用的遷移」在命令提示符下(我也嘗試了新的「添加遷移」附帶EF7 - 因爲EF7在我的Web項目,我雖然我會試試看引用)。

以下是錯誤消息我得到:

異常調用 「的SetData」 與 「2」 參數(S):「類型「Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package。 Automation.OAProject」在組件‘Microsoft.VisualStudio.ProjectSystem.VS.Implementation,版本= 14.0.0.0,文化=中性公鑰= b03f5f7f11d50a3a’未標記爲可序列「。在C:\ Tfs \ Dev \ primaVISTA \ packages \ EntityFramework.6.1.3 \ tools \ EntityFramework.psm1:720 char:5 + $ domain.SetData('startUpProject',$ startUpProject) + ~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:NotSpecified :(:) [],MethodInvocationException + FullyQualifiedErrorId:SerializationException System.NullReferenceException:對象不設置爲一個對象的一個​​實例。 at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project,Int32 shellVersion) at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project) 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。 <。> b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand。執行(操作命令) 未將對象引用設置爲對象的實例。

有沒有人知道我做錯了什麼?我錯過了什麼?或者,這是由於VS2015中的錯誤?

我真的很感謝任何幫助!謝謝!!


正如Sirwan建議的那樣,我打開了一個命令提示符,並將自己的方式放到了我的庫項目文件夾中。在這裏,我執行添加遷移命令:

C:\Tfs\Dev\primaVISTA\src\primaVISTA.Core>dnx . ef migration add M1 

System.InvalidOperationException:無法解析項目 'primaVISTA.Core' 從C:\ TFS \開發\ primaVISTA的\ src \ primaVISTA.Core 在Microsoft.Framework.Runtime .ApplicationHostContext..ctor(的IServiceProvider的ServiceProvider,字符串projectDirectory,字符串packagesDirectory,字符串配置,FrameworkName targetFramework,ICACHE緩存,ICacheContextAccessor cacheContextAccessor,INamedCacheDependencyProvider namedCacheDependencyProvider,IAssemblyLoadContextFactory loadContextFactory,布爾skipLockFileValidation) 在Microsoft.Framework.Runtime.DefaultHost.Initialize(DefaultHostOptions選項,IServiceProvider hostServices) at Microsoft.F ramework.Runtime.DefaultHost..ctor(DefaultHostOptions選項,IServiceProvider hostServices) at Microsoft.Framework.ApplicationHost.Program.Main(String [] args) ---上一個位置拋出異常的堆棧跟蹤結束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly assembly,String [] args,IServiceProvider serviceProvider) at dnx.host.Bootstrapper.RunAsync(List` 1個指定參數時,IRuntimeEnvironment ENV,FrameworkName targetFramework) 在dnx.host.RuntimeBootstrapper.ExecuteAsync(字串[] args,FrameworkName targetFramework) 在dnx.host.RuntimeBootstrapper.Execute(字串[] args,FrameworkName targetFramework)

我不知道這是什麼意思?

回答

3

這不是目前在Visual Studio 2015年的情況下,你應該鍵入您的項目目錄下(不是您的解決方案目錄):

dnx . ef [options] [command] 

要查看子命令可用於遷移命令,dnx . ef migration --help類型:

  • add - 添加新的遷移

  • apply - 應用遷移到數據庫

  • list - 列出遷移
  • script - 生成遷移
  • remove SQL腳本 - 刪除最後一個遷移

more info

更新:所有的 首先,你必須設置你的環境:

  1. 打開一個新的命令提示符。
  2. 導航到src [ProjectName]目錄。
  3. 運行dnvm升級以安裝必要的ASP.NET 5工具,如果尚未完成。
  4. 運行dnu restore加載項目所需的所有軟件包。

然後運行這個命令:

dnx . ef migration add firstMigration 
dnx . ef migration apply 
+0

我試圖按照您的建議 - 並相應更新我原來的職位(請參見上文) – Ingmar

+0

好吧,我想,我有一個一般的誤解:如果我執行「dnx。ef migration add M1」不在我的圖書館項目中,但在我的web項目中,一切都很好。然後,在運行「dnx.ef migration apply」之後,我的數據庫被創建。歡呼!但是:它只包含ASP.NET Identity的實體。我的自定義實體(在我的庫項目中定義)都沒有在數據庫中創建。 – Ingmar

+0

@IngmarBode我已經更新了我的答案,查看我的答案 –