Skip to main content
 首页 » 编程设计

entity-framework之在 x64 项目中启用迁移获取 System.BadImageFormatException

2024年11月24日30xiaohuochai

我有一个设置为 x64 的项目(它使用了一些仅 64 位的 Nuget 包)。一切运行和部署正常,但试图运行 EF 的 enable-migrations在包管理器控制台给我一个 System.BadImageFormatException .完整的异常(exception):

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. 

注意:我已经从错误消息中删除了项目名称,这既是为了让谷歌更容易搜索,也是因为它与这个问题无关。

请您参考如下方法:

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

将您的项目构建路径更改为 /bin/Debug 是无害的神奇的是,一切都开始像它应该的那样工作。

错误一直存在到 EF 6.1.0(包括 EF 6.1.0)。 Bug report posted .

更新 :Microsoft 已决定不修复该错误,已关闭为 wontfix,因为存在解决方法。相当恶劣的行为。