Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't Enable-Migrations. Throws System.BadImageFormatException: Could not load file or assembly

I can't enable EF migrations!

Using the package manager console, it throws the following:

PM> Enable-Migrations System.BadImageFormatException: Could not load file or assembly 'MyApp' or one of its dependencies. Index not found. (Exception from HRESULT: 0x80131124) File name: 'MyApp' ---> System.BadImageFormatException: Index not found. (Exception from HRESULT: 0x80131124) at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) at System.Reflection.Assembly.Load(String assemblyString) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.LoadAssembly() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindType[TBase](String typeName, Func2 filter, Func2 noType, Func3 multipleTypes, Func3 noTypeWithName, Func3 multipleTypesWithName) at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypeRunner.RunCore() at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()

Could not load file or assembly 'MyApp' or one of its dependencies. Index not found. (Exception from HRESULT: 0x80131124)

Also:
1. My default project (in the PM CONSOLE) is 'MyApp'
2. The solution only has 'MyApp'
3. The class inheriting from DbContext is in 'MyApp.Models'
4. I also tried creating a new solution and then copy pasted all classes to it and it threw the same error

What is happening?
I once enabled migrations in this project in the past, but two days later I deleted all migration stuff from it because it wasn't necesary. But now I really need them

like image 426
sports Avatar asked Jun 01 '13 15:06

sports


2 Answers

I just had the same problem.

The reason for the System.BadImageFormatException was because I had a dependency on an x64 DLL (Magick.NET-x64.dll in my case). Forcing the project to build in 32-bit solved it.

  1. Select your WebProject
  2. Go to Properties
  3. Select the Build tab
  4. Change the Platform target: from x64 to Any CPU

Hopefully this helps someone else.

like image 67
Matthew Perron Avatar answered Oct 23 '22 06:10

Matthew Perron


It depends of the current output folder of your project. When changing the target platform to x64 the output folder also changes to bin\x64\Debug. This seemed to be a problem for the enable-migrations cmdlet. After changing the output folder back to bin\Debug for a x64 platform build everything works fine.

found at: http://entityframework.codeplex.com/discussions/438488

like image 39
Henry Peltzer Avatar answered Oct 23 '22 06:10

Henry Peltzer