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

时间:2014-05-14 23:15:57

标签: entity-framework 64-bit ef-migrations

我有一个项目设置为x64(它使用的是一些64位的Nuget软件包)。一切都运行并部署得很好,但是尝试在软件包管理器控制台上运行EF 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 个答案:

答案 0 :(得分:39)

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

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

Bug存在并包括EF 6.1.0。 Bug report posted

更新:Microsoft已决定不再修复该错误,因wontfix而关闭,因为存在一种解决方法。非常糟糕的行为。

答案 1 :(得分:5)

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

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

答案 2 :(得分:2)

核心问题围绕着x64。在我的场景中,我尝试使用Azure Mobile Services Todo参考解决方案。在此解决方案中,有一个服务解决方案和2个前端客户端(Windows 8和Windows Phone)解决方案。

在试验解决方案时,我添加了离线支持。脱机支持实现SQLite,SQLite需要配置解决方案以使用x64或ARM用于相应的本机客户端。

因此,更改CPU目标以解决SQLite支持,我必须在服务上修改CPU x64并保存。当我看到我使用x64进行服务的错误时,我改回了任何CPU。

这一切都导致...当我下次尝试添加迁移时,我的服务的.csproj文件被损坏了#34;根据这篇文章中的信息。在我的情况下,我在服务的.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。

答案 3 :(得分:0)

转到IIS管理器&gt;查看应用程序池&gt;默认应用程序工具&gt;高级设置...&gt;将32位设置为true

答案 4 :(得分:0)

不要像我一样困惑:

  1. ConfigurationManager Active Platform 更改为 AnyCPU (如果它已丢失 - 添加)
  2. 转到包含 DbContext
  3. 的项目的属性
  4. 构建标签中,将平台目标更改为 AnyCPU
  5. 程序包管理器控制台中运行以下命令:Enable-Migrations -ProjectName ProjectNameThatContainsTheDContext