在x64项目中启用迁移会导致System.BadImageFormatException错误

25

我的项目设置为x64(使用了一些只支持64位的Nuget包)。一切正常运行和部署,但试图在包管理器控制台上运行EF's enable-migrations会出现System.BadImageFormatException异常。完整的异常信息:

PM> enable-migrations
System.BadImageFormatException: Could not load file or assembly  or one of its dependencies. An attempt was made to load a program with an incorrect format.
File name: 
   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(String name)
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypeRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextType(String contextTypeName)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].

Could not load file or assembly  or one of its dependencies. An attempt was made to load a program with an incorrect format.
注意:我已从错误信息中删除了项目名称,这样可以更轻松地进行谷歌搜索,并且因为它与此问题无关。
5个回答

46
问题在于,enable-migrations 命令似乎有一个硬编码的路径,在此路径下,EF会查找你的项目构建DLL的位置,路径为 /bin/Debug,而不管实际的构建路径是什么。当你将项目更改为x64时,Visual Studio会悄悄地将你的项目构建路径更改为/bin/x64/Debug,而EF仍然会在/bin/Debug中查找,导致了这种模糊的 System.BadImageFormatException 异常。
只需将项目构建路径更改为 /bin/Debug 即可,这是无害的,神奇的是,一切都开始按预期工作了。
Bug 存在于 EF 6.1.0 及以下版本。已发布 Bug 报告更新: Microsoft 决定不修复该错误,因为存在解决方法,因此被关闭。这种行为相当糟糕。

4
需要将目标项目及其依赖平台改为x86或Any CPU,仅更改输出路径是不够的。当PM控制台尝试在Visual Studio的32位进程中加载x64 Assembly时,会出现BadImageFormatException异常。(我只是在重复MS在错误报告中对该错误的评论。) - Epstone
更改bin目录对ASP.NET项目不适用 - 只有一个bin目录,所有内容都在其中。这是“Enable-Migrations”仅与x86程序集配合工作的问题。将项目的输出类型更改为“AnyCPU”即可解决问题。 - Phil

7

根据微软支持的说法:“解决方法是在生成/运行迁移之前切换到 AnyCPU 或 x86,然后再切换回来。”

他们还建议:“将模型/迁移重构为一个独立的 AnyCPU 项目”。


重构迁移至 AnyCPU 项目是不错的,但为了解决问题需要付出很多努力。 - Heiner
3
当我的项目中只有x64依赖项时,这并没有帮助到我... - JonathanPeel

2
核心问题在于x64。在我的场景中,我试图使用Azure移动服务Todo参考解决方案。这个解决方案中有一个服务解决方案和两个前端客户端(Windows 8和Windows Phone)解决方案。
在尝试解决方案时,我添加了离线支持。离线支持实现了SQLite,并且SQLite需要配置解决方案以使用x64或ARM用于各自的本机客户端。
因此,为了解决SQLite支持而更改CPU目标,我必须在服务上修改CPU x64并保存。当我发现我错误地使用x64作为服务时,我改回了Any CPU。
这一切导致了...下一次我尝试添加迁移时,我的服务.csproj文件基于这篇文章中的信息被“损坏”。在我的情况下,我在服务.csproj文件中进行了以下更改/更新:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

在我的情况下,
<PlatformTarget/> 

仍然设置为x64。


-1

不要像我一样感到困惑:

  1. ConfigurationManager中更改Active PlatformAnyCPU(如果缺少-请添加)
  2. 进入包含DbContext的项目的属性
  3. 生成选项卡中,将平台目标更改为AnyCPU
  4. Package Manager Console中运行以下命令:Enable-Migrations -ProjectName ProjectNameThatContainsTheDContext

-2

进入 IIS 管理器 > 查看应用程序池 > 默认应用程序工具 > 高级设置... > 将 32 位设置为 true


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接