Asp.net-Mvc-4

升級到 .NET 4.5 和 EF 5 後,“啟用遷移”失敗

  • July 8, 2015

我剛剛將我的 MVC4 項目升級到 .NET 4.5 和 EF5 並開始使用 VS2012。在意識到我需要再次在包管理器中設置自動遷移後,我執行Enable-Migrations - EnableAutomaticMigrations並收到錯誤

No context type was found in the assembly 'MySolutionName'.

一些研究表明這與 EF5 未啟用預發布有關。我跑了Install-Package EntityFramework -IncludePrerelease,但它說 EF5 已經安裝(這是我之前通過 NuGetmanager 安裝它時沒有指定-IncludePrerelease.

有誰知道我必須做什麼才能為我的項目啟用遷移?

我剛遇到同樣的問題,在尋找解決方案時發現了你的問題。

我讓它工作了。對我來說,問題是當我通過 NuGet 添加 EF 5 時,我最初的目標是 .NET 4.0 框架。更改目標框架,然後通過 NuGet 重新安裝 EF 5,修復它。也有可能(見評論)通過 NuGet 重新安裝 EF 5 是您的解決方案。

我在 App.config 文件中有以下行,注意 Version=4.4.0.0:

<configuration>
 <configSections>
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 </configSections>
</configuration>

所以我所做的是在解決方案配置中將目標框架設置為 4.5,並將支持的執行時也設置為 4.5(在應用程序配置中)。

老的:

 <startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
 </startup>

新的:

 <startup>
   <supportedRuntime version="v4.5" sku=".NETFramework,Version=v4.5" />
 </startup>

更改後,我通過 NuGet 刪除了 EF 5.0 並再次添加了它。結果它給了我以下 configSection,注意 Version=5.0.0.0:

<configuration>
 <configSections>
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
 </configSections>
</configuration>

更改後,它起作用了。

引用自:https://stackoverflow.com/questions/12021656