实体框架代码首次迁移抛出错误

时间:2012-12-12 03:44:14

标签: entity-framework c#-4.0 ef-code-first database-migration

以下是我在收到此错误之前所做的事情:

  1. 安装EF(预发布版本,6可能,我不知道)
  2. 决定我不想要,所以卸载并重新安装稳定版本(5)
  3. 通过SSMS截断我的数据库
  4. 这是错误:

    Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or      
    assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicK
    eyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest   
    definition does not match the assembly reference. (Exception from H
    RESULT: 0x80131040)"
    At C:\Projects\BCATracker.Web\packages\EntityFramework.6.0.0- 
    alpha2\tools\EntityFramework.psm1:425 char:31
    +     $domain.CreateInstanceFrom <<<< (
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodTargetInvocation
    

    以下是我的尝试:

    • 从bin目录中删除everthing,重建。
    • 从nuget中删除EF,删除packages目录中的文件,在nuget中读取ef。

    查看proj文件,我看到了:

    <Reference Include="EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\packages\EntityFramework.5.0.0\lib\net40\EntityFramework.dll</HintPath>
    </Reference>
    

    可能的根本原因是什么?

9 个答案:

答案 0 :(得分:16)

我弄明白了这个问题。 EntityFramework.dll需要在GAC中供Visual Studio从包管理器中使用它。

  1. 启动Visual Studio命令提示符(确保以管理员身份启动Visual Studio)
  2. 转到您的packages目录并找到EntityFramework包目录。
  3. 转到lib \ net45(如果定位4.5)
  4. 类型:gacutil / i EntityFramework.dll
  5. 注意:此时完全重启计算机可能是明智的。

    我不确定为什么Visual Studio会停止看到DLL,但这让我解决了这个问题。

答案 1 :(得分:2)

是的,上面的评论是正确的,对我有用。

EntityFramework.dll需要在GAC中供Visual Studio从包管理器中使用它。

1. Start Visual Studio Command Prompt (ensure you started Visual Studio as Administrator) 
2. Go to your packages directory and find the    EntityFramework package directory. 
3. Go to lib\net45 (if targeting 4.5)    
4. type: gacutil /i EntityFramework.dll 

注意:此时完全重启计算机可能是明智的。

C:\Windows\system32>cd..

C:\Windows>cd..

C:\>cd \packages\EntityFramework.5.0.0\lib\net45

C:..\packages\EntityFramework.5.0.0\lib\net45>
gacutil /i EntityFramework.dll

这肯定会奏效。

答案 2 :(得分:1)

我在使用代码首次迁移时也遇到了这个问题。只需将实体框架版本更新为Entity Framework V6.1.3即可解决此问题。 在包管理器控制台“Install-Package Entity Framework -version 6.1.3”上使用命令

答案 3 :(得分:1)

  1. 打开管理NUGET包以获取解决方案...窗口并更新EntityFramework
  2. 重新启动Visual Studio
  3. 在程序包管理器控制台中运行命令启用 - 迁移--ContextTypeName Movie.Models.MovieDBContext(这是示例上下文)

答案 4 :(得分:1)

安装EF 6.1.3,这将解决问题。

答案 5 :(得分:0)

我相信我可以找到解决方案。我的解决方案中有几个项目。

enter image description here

我的数据库逻辑在SqlDataAccess中。该程序集在WebUI中引用。

我从DomainLogic开始。在那里为存储库基类定义POCO。

然后我添加了SqlDataAccess项目。

并尝试Enable-Migrations, Add-MigrationUpdate-Database命令。它运行正常。我添加了WebUI(基于MVC 4的Web应用程序到项目并运行上面的命令。我开始得到这个版本错误。

我卸载项目WebUI并重新运行命令。它再次运行良好。因此,即使它不是最佳解决方案,您可能希望卸载其他项目,将atabase项目留在解决方案中,然后运行这些命令。它对我有用。一切顺利!

答案 6 :(得分:0)

执行以下操作以解决此问题:

1)从包中卸载Entity Framework(管理NUGET包)。

2)重新启动Visual Studio。

3)从包管理器再次重新安装Entity Framework。

4)添加对System.Data.Entity的引用。

5)在包管理器控制台中运行命令Enable-Migrations -ContextTypeName Movie.Models.MovieDBContext(这是一个示例上下文)。

在此之后,您可以看到packages.config中反映的更改。

答案 7 :(得分:0)

我有Windows 10.以下步骤纠正了问题

  1. 打开“程序和功能”并搜索“实体”
  2. 我注意到在2015年7月22日的6.1.3更新后的2016年1月23日安装了更新
  3. 选择'实体框架6.1.3'等。更新
  4. 点击“修复”按钮。修复完成后,我重新启动Visual Studio并且不再浮出水面。
  5. 感谢@Farshid提供解决方案的初步线索。

    enter image description here

答案 8 :(得分:-1)

我也遇到了同样的错误,浪费了很多时间来处理这个错误 **这只是因为我更新了实体框架,但它给出了这个错误,所以我再次安装 Entity Framework 6.0.0 ** 错误已解决

enter image description here