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

4

5 回答 5

5

我只是有同样的问题。

的原因System.BadImageFormatException是因为我依赖于 x64 DLL(Magick.NET-x64.dll在我的情况下)。强制项目构建 32 位解决了它。

  1. 选择您的网络项目
  2. 转到属性
  3. 选择构建选项卡
  4. 更改平台目标:从x64Any CPU

希望这对其他人有帮助。

于 2015-09-02T22:19:47.680 回答
1

这取决于项目的当前输出文件夹。将目标平台更改为 x64 时,输出文件夹也会更改为 bin\x64\Debug。这似乎是 enable-migrations cmdlet 的问题。将输出文件夹更改回 bin\Debug 以进行 x64 平台构建后,一切正常。

位于:http ://entityframework.codeplex.com/discussions/438488

于 2015-09-20T18:52:46.370 回答
0

我有一些东西可以帮助你...

这篇 文章也有类似的例外。

并且MSDN提出了类似的问题(见底部的注释)。

如果我是你,我会尝试按照另一篇文章中的步骤进行操作,也可能是“清洁解决方案”。在我看来,PM 在执行 cmdlet 时遇到了与该帖子类似的问题,因此我将检查 MyApp.Properties 下的所有设置。

希望你能找到解决办法。

于 2013-06-06T16:45:51.127 回答
0

我终于找到问题所在了!

我所做的是创建一个全新的解决方案。然后我开始从原始解决方案中复制/粘贴/“包含在项目中”每个 .cs。每次粘贴时,我都尝试运行“Enable-Migrations”命令。每次粘贴它都运行良好,直到我发现导致 Enable-Migrations 没有运行的有问题的 .cs。

然后,在有问题的 .cs(它是一个控制器)中,我开始隔离代码以查看导致问题的原因。

这是产生差异的代码(Enable-Migrations ok vs badimageformatexception 等)

    /// <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);
    }
于 2013-06-12T00:47:47.730 回答
0

尝试以下操作:

  • 检查解决方案中引用实体框架的所有项目是否引用相同的版本,

  • 卸载任何不包含应用程序代码的项目,例如设置/部署或文档项目,以及

  • 暂时禁用任何 Visual Studio 加载项。

然后重新启动 Visual Studio 并再次执行该命令。另外,看看以管理员身份运行 VS 是否有帮助。

于 2013-06-07T16:00:40.053 回答