13

I am trying to enable Code First in my Visual Studio Web Express 2012. From what I have read so far I have to use the Package Manager Console and the command Enable-Migrations. When I do this I get the following error.

Exception calling "LoadFrom" with "1" argument(s): "Could not load file or assembly 'file:///E:\Working\wingtoysdl\C#\packages\EntityFramework.5
.0.0\tools\EntityFramework.PowerShell.Utility.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)"
At E:\Working\wingtoysdl\C#\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:398 char:62
+     $utilityAssembly = [System.Reflection.Assembly]::LoadFrom <<<< ((Join-Path $toolsPath EntityFramework.PowerShell.Utility.dll))
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At E:\Working\wingtoysdl\C#\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:399 char:50
+     $dispatcher = $utilityAssembly.CreateInstance <<<< (
    + CategoryInfo          : InvalidOperation: (CreateInstance:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "CreateInstanceFrom" with "8" argument(s): "Could not load file or assembly 'file:///E:\Working\wingtoysdl\C#\packages\EntityF
ramework.5.0.0\tools\EntityFramework.PowerShell.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515
)"
At E:\Working\wingtoysdl\C#\packages\EntityFramework.5.0.0\tools\EntityFramework.psm1:431 char:31
+     $domain.CreateInstanceFrom <<<< (
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

As you can see it is a very large error making it very hard to google and troubleshoot. Does anyone have any ideas on how I can fix this?

jackncoke
  • 2,000
  • 6
  • 25
  • 66

7 Answers7

29

I've run into this issue twice now. Both times I've had to uninstall entity framework then re-install it for each project in the solution. One thing that took me a while to figure out the second time I did this is that I had to re-start Visual Studio (2012) after re-installing Entity Framework (or I continued to get the "Could not load file or assembly '[path]\packages\EntityFramework.5 .0.0\tools\EntityFramework.PowerShell.Utility.dll'" error).

To get to the Package Manager Console go to: Tools -> Library Package Manager -> Package Manager Console

In the Package Manager uninstall Entity Framework for each project (I selected each project from the Default Project dropdown at the top of the Package Manager Console):
Uninstall-Package EntityFramework -Force

Then install it for each project:
Install-Package EntityFramework

At this point attempting to run the Enable-Migrations command gave the error still, until I closed and restarted Visual Studio. Then it was successful, as was the Update-Database command (after setting AutomaticMigrationsEnabled to true in the Migrations.Configuration file).

Torrents
  • 505
  • 7
  • 11
  • 1
    Can anyone shed some light on why this happens? I have encountered it regularly. – GDB Feb 01 '14 at 20:44
  • 3
    @GDB: In our case, the EF DLL was blocked because it came from a zip file (we were restoring a copy of a beta release of the package). We had to go to file properties and select "unblock" to disable the Windows file protection. This leads me to believe that it can occur in some cases due to simple permissions errors. – mwilson Feb 12 '14 at 19:15
  • 2
    I found this worked because as I was uninstalling EF from my projects, for some reason in my Test project I had a different, later version of EF. I don't know how it got there but this seemed to cause the error. – arame3333 Jan 20 '15 at 07:34
5

Had the same issue on two different machines while using projects on a network drive. This answer fixed it for me.

Community
  • 1
  • 1
Reid Rankin
  • 1,078
  • 8
  • 26
3

This may be a late answer but this seems like it could be a permissions/access problem. Make sure that your project is not being opened from a mapped drive or similar.

If you are on a corporate network and logged into a domain, it is very likely that your user profile does not actually sit on your local computer but rather on the server. This will cause the same problem.

Try copy the project to your local c: and try again.

Talon
  • 3,466
  • 3
  • 32
  • 47
2

My problem was caused by some projects in my solution not being updated to the same version as other projects. When I ensured all were on the same EF version, it began working.

DLeh
  • 23,806
  • 16
  • 84
  • 128
1

So I had the same problem for a while, I was unpacking a working project and got the error as I tried to enable migrations. It was simply fixed by using 7-Zip instead of the default unzipper from windows 10

Djohnser
  • 41
  • 2
1

I was also encounter the same problem so I started to check the possible things could go wrong and I found 2 different versions of entity framework is get installed somehow.

To confirm I goto Tools> NuGet package manager solution > Manage NuGet packages > Consolidate.

Have a look below images.

enter image description here

In this picture you may clearly see the different version is installed

enter image description here

in this picture I am agree to update the Entity Framework from 6.2.0 to 6.4.4

Rookie-devRaj
  • 91
  • 1
  • 1
  • 7
0

I had the same issue and in my case it was network drive problem. When I copied my project in local drive it worked fine.

Megha Misra
  • 35
  • 1
  • 7
  • 1
    This post isn't an actual attempt at answering the question. Please note [StackOverflow doesn't work like a discussion forum](http://stackoverflow.com/tour), it is a Q&A site where every post is either a question or an answer to a question. Posts can also have [comments](http://stackoverflow.com/help/privileges/comment) - small sentences like this one - that can be used to critique or request clarification from an author. This should be either a comment or a [new question](http://stackoverflow.com/questions/ask) – ρяσѕρєя K Jan 07 '20 at 17:12