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

7

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

entity-framework
asp.net-mvc-4
entity-framework-migrations
asked on Stack Overflow Jun 1, 2013 by sports

5 Answers

4

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.

answered on Stack Overflow Sep 2, 2015 by Matthew Perron
1

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

answered on Stack Overflow Sep 20, 2015 by Henry Peltzer
0

I have some things that may help you...

This post has a similar exception.

And MSDN suggest similar issues (see remarks at the bottom).

If I were you I would try following the steps in the other post and maybe "Clean Solution" as well. It seems to me that the PM is having trouble executing the cmdlet because of something similar to that post so I would check all the settings under MyApp.Properties.

Hope you find a solution.

answered on Stack Overflow Jun 6, 2013 by Dave Williams
0

Try the following:

  • check that all projects in your solution that reference Entity Framework reference the same version,

  • unload any projects that do not contain code for your application, e.g. setup/deployment or documentation projects, and

  • temporarily disable any Visual Studio add-ons.

Then restart Visual Studio and execute the command again. Also, see if running VS as an administrator helps.

answered on Stack Overflow Jun 7, 2013 by Olly
0

I finally found what the problem was!

What I did was create a whole new solution. Then I started to copy/paste/"Include in Project" every .cs from the original solution. With every paste I tried to run the "Enable-Migrations" command. With every paste it was working fine until I found the problematic .cs that caused that Enable-Migrations didn't run.

Then, inside the problematic .cs (which was a Controller), I started isolating code to see what was causing the problem.

This was the code that made the difference (Enable-Migrations ok vs badimageformatexception etc etc)

    /// <summary>
    /// GET: /MessagesAjax/SendWithInetlab/?to=56995189711&body=bla
    /// </summary>
    [JsonHandleError]
    public JsonResult SendWithInetlab(string to, string body) // TODO: Delete
    {
        Inetlab.SMPP.SmppClient client = new Inetlab.SMPP.SmppClient();
        client.Connect("200.54.98.222", 54002);

        // TODO: test
        if (client.Status == Inetlab.SMPP.Common.ConnectionStatus.Open)
        {
            client.Bind("blah", "pwd", Inetlab.SMPP.Common.ConnectionMode.Transmitter);

            if (client.Status == Inetlab.SMPP.Common.ConnectionStatus.Bound)
            {
                IList<SubmitSmResp> respList = client.Submit(
                    SMS.ForSubmit().From("56951000001").To(to).Text(body)
                    );

                client.UnBind();
            }
            client.Disconnect();
        }

        return Json("bla", JsonRequestBehavior.AllowGet);
    }
answered on Stack Overflow Jun 12, 2013 by sports

User contributions licensed under CC BY-SA 3.0