Entity Framework code first migrations throwing er

2019-03-25 09:33发布

Here is what I did before I got this error:

  1. Installed EF (the pre release version, 6 maybe, I dunno)
  2. decided I did not want, so uninstalled that and reinstalled the stable version (5)
  3. Truncated my database via SSMS

here is the error:

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

Here is what I have tried:

  • Delete everthing from the bin directory, rebuild.
  • remove EF from nuget, delete the file in the packages directory, readd ef in nuget.

Looking at the proj file I see this:

<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>

What could be a possible root cause?

9条回答
男人必须洒脱
2楼-- · 2019-03-25 09:40

I also faced this problem while using using code first migration. just update the entity framework version to Entity Framework Version 6.1.3 to resolve this. using command on Package manager console "Install-Package Entity Framework -version 6.1.3"

查看更多
萌系小妹纸
3楼-- · 2019-03-25 09:41

I figured out the problem. EntityFramework.dll needed to be in the GAC for Visual Studio to use it from the package manager.

  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

Note: It may be wise to restart the computer entirely at this point.

I'm not sure why Visual Studio stopped seeing the DLL, but this got me past this problem.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-03-25 09:41

Yes above comment is right that worked for me.

EntityFramework.dll needed to be in the GAC for Visual Studio to use it from the package manager.

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 

Note: It may be wise to restart the computer entirely at this point.

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

This will definitively works.

查看更多
Rolldiameter
5楼-- · 2019-03-25 09:41

Perform the following actions to resolve this issue:

1)Uninstall Entity Framework from the package(Manage NUGET Packages).

2)Restart Visual Studio.

3)Re-install Entity Framework again from package manager.

4)Add reference to the System.Data.Entity.

5)Run the command in package manager console Enable-Migrations -ContextTypeName Movie.Models.MovieDBContext(This is a sample context).

After this you could see the changes reflected in packages.config.

查看更多
爷、活的狠高调
6楼-- · 2019-03-25 09:46

I have Windows 10. The following steps corrected the issue

  1. Open Program and Features and do a search for 'entity'
  2. I noticed an update was installed on 1/23/2016 after the 6.1.3 update on 7/22/2015
  3. Selected the 'Entity Framework 6.1.3' etc.. update
  4. Clicked on the "Repair" button. After the repair completed I restarted Visual Studio and issue no longer surfaced.

Thanks to @Farshid for the initial clue to a resolution.

enter image description here

查看更多
欢心
7楼-- · 2019-03-25 09:48

Install EF 6.1.3 and this will solve the problem.

查看更多
登录 后发表回答