x64 Project中的enable-migrations获取System.BadImageFormatException

Chr*_*ini 24 64-bit entity-framework ef-migrations

我有一个项目设置为x64(它使用一些64位的Nuget包).一切都运行和部署良好,但尝试enable-migrations在包管理器控制台运行EF's 得到我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.
Run Code Online (Sandbox Code Playgroud)

注意:我已从错误消息中删除了项目名称,这两者都使谷歌更容易,因为它与此问题无关.

Chr*_*ini 42

问题是该enable-migrations命令似乎有一个硬编码路径,其中EF查找项目的构建DLL /bin/Debug,无论实际构建路径是什么.当您将项目更改为x64时,Visual Studio会悄悄地将项目的构建路径更改为/bin/x64/Debug- 而EF一直在查找/bin/Debug.这导致了这种模糊的System.BadImageFormatException.

只是改变你的Project构建路径/bin/Debug并且神奇地,一切都开始像它应该的那样开始工作是无害的.

Bug存在并包括EF 6.1.0.错误报告发布.

更新:微软决定不打扰修复bug,因为存在解决方法,因此关闭为wontfix.非常不好的行为.

  • 有必要将目标项目及其依赖项的平台更改为x86或任何CPU.更改输出路径是不够的.当PM控制台尝试在Visual Studio的32位进程中加载​​x64程序集时,会创建BadImageFormatException.(我只是在bug报告中重复关于MS的bug的评论.) (4认同)

Ada*_*Cox 6

如Microsoft支持所述:“解决方法是交换到AnyCPU或x86(在您生成/运行迁移之前,然后交换回去)。”

他们还建议:“将模型/迁移[重构]到一个单独的项目AnyCPU中”

  • 当我的项目中只有 x64 依赖项时,这没有帮助... (2认同)