Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework code first migrations throwing error

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?

like image 586
ledgeJumper Avatar asked Dec 12 '12 03:12

ledgeJumper


People also ask

How do I get rid of migrations in Entity Framework?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.


2 Answers

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.

like image 199
allen1 Avatar answered Sep 22 '22 11:09

allen1


I believe I could figure out a solution. I had several projects in my solution.

enter image description here

My database logic was in SqlDataAccess. The assembly was reference in WebUI.

I started off at the DomainLogic. Define POCOs for repository base classes there.

Then I added SqlDataAccess project.

and tried Enable-Migrations, Add-Migration and Update-Database command. It ran fine. After I added WebUI (an MVC 4 based web application to the project and ran the commands above. I started getting this version error.

I unload the project WebUI and re ran the command. And it ran fine again. So, Even if it's not the best solution you might want to unload other projects leaving the atabase project in the solution and then running these commands. It worked for me. All the best!

like image 23
Ziaullah Khan Avatar answered Sep 22 '22 11:09

Ziaullah Khan