14

我刚刚将我的 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.

有谁知道我必须做什么才能为我的项目启用迁移?

4

2 回答 2

15

我刚遇到同样的问题,在寻找解决方案时发现了你的问题。

我让它工作了。对我来说,问题是当我通过 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>

更改后,它起作用了。

于 2012-08-30T14:34:26.003 回答
1

我也遇到了同样的问题,但一天后我突然注意到 ASP MVC 4 项目文件夹中名为 packages.config 的文件。所以,我得到了

 <package id="EntityFramework" version="6.0.2" targetFramework="net45" />

我将版本号更改为适当的数字,即我使用的是 EF 版本 5.0.0。在我更改版本后,现在一切都很好。

于 2013-12-19T07:43:18.563 回答