无法启用 - 迁移.引发System.BadImageFormatException:无法加载文件或程序集

spo*_*rts 7 entity-framework asp.net-mvc-4 ef-migrations

我无法启用EF迁移!

使用包管理器控制台,它会抛出以下内容:

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, Func在System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()的System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypeRunner.RunCore()中有2个过滤器,Func 2 noType, Func3 multipleTypes,Func 3 noTypeWithName, Func3 multipleTypesWithName)

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

另外:
1.我的默认项目(在PM CONSOLE中)是'MyApp'2
.解决方案只有
'MyApp'3.继承自DbContext的类在'MyApp.Models'中
4.我也尝试创建一个新的解决方案然后复制粘贴所有类到它,它抛出相同的错误

怎么了?
我曾经在此项目中启用了迁移,但两天后我删除了所有迁移内容,因为它不是必需的.但现在我真的需要它们

Mat*_*ron 5

我只是遇到了同样的问题。

原因System.BadImageFormatException是因为我依赖于 x64 DLL(Magick.NET-x64.dll就我而言)。强制项目以 32 位构建解决了它。

  1. 选择您的Web 项目
  2. 转到属性
  3. 选择构建选项卡
  4. 更改平台目标:从x64任何 CPU

希望这对其他人有帮助。


spo*_*rts 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);
    }
Run Code Online (Sandbox Code Playgroud)