带有NHibernate的System.Data.SQLite BadImageFormatException,在独立时可以正常工作 [英] System.Data.SQLite BadImageFormatException with NHibernate, works finewhen standalone

查看:136
本文介绍了带有NHibernate的System.Data.SQLite BadImageFormatException,在独立时可以正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将NHibernate与SQLite结合使用, .NET 4和32位的版本= 1.0.74.0. 我使用64位WIN7,但以x86模式构建应用程序 (VS2010 Express中的默认设置).

I tried to use NHibernate with SQLite, version=1.0.74.0 for .NET 4 and 32 bit. I use a 64bit WIN7, but build the application in x86 mode (default in VS2010 express).

当我使用与独立应用程序相同的SQLite时,它可以正常工作,但是当 我尝试将其与NHibernate一起使用,它会抛出BadImageFormatExcepion

When I use the same SQLite as a standalone application it works fine, but when I try to use it with NHibernate it throws BadImageFormatExcepion

我调试了NHibernate并在以下语句中引发了异常

I debugged a bit NHibernate and the Exception is thrown at the folllowing statement

System.Type.GetType("System.Data.SQLite.SQLiteConnection, System.Data.SQLite");

有人知道解决方案吗? 是因为我在VS Express中使用默认模式吗? 我是否需要使用其他方法指定平台? 尝试为sqlite下载其他dll吗?

Any chances somebody knows the solution? Is it because I use default mode in VS Express? Do I need to specify platform using some other method? try to download some other dll for sqlite?

我尽可能地检查了其他答案 将此添加到我的app.config中:

I checked some other answers on SO thebest I got was to add to my app.config this:

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
    </DbProviderFactories>
  </system.data>



 <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
  </startup>

推荐答案

如果VS express不允许您选择平台,则可以尝试在.csproj文件中手动进行设置

If VS express does not allow you to choose a platform you can try setting it up manually in .csproj file

<PlatformTarget>x86</PlatformTarget>

构建应用后,请确保:

  • 它作为32位或64位应用程序执行,具体取决于您放置在PlatformTarget中的内容(使用Windows Task Manager或Process Explorer)
  • 正确版本的 SQLite.Interop.dll 复制到您拥有exe的文件夹中(此dll依赖于平台,因此您需要复制与EXE平台对应的版本)
  • 已安装 Visual C ++ 2010 SP1可再发行组件包的相应版本
  • it is executed as 32 or 64 bit app depending on what you put in PlatformTarget (using Windows Task Manager or Process Explorer)
  • the right version of SQLite.Interop.dll is copied to the folder where you have your exe (this dll is platform dependent, so you need to copy the version corresponding to EXE platform)
  • Corresponding version of Visual C++ 2010 SP1 Redistributable Package is installed

也请尝试从配置中删除useLegacyV2RuntimeActivationPolicy.

Also try removing useLegacyV2RuntimeActivationPolicy from config.

来自 BadImageFormatException 文档:

... DLL或可执行文件作为64位程序集加载,但包含 32位功能或资源.例如,它依赖于COM互操作或 调用32位动态链接库中的方法. ...要解决此异常,请将项目的平台"目标属性设置为x86(而不是x64或AnyCPU)并重新编译.

... A DLL or executable is loaded as a 64-bit assembly, but it contains 32-bit features or resources. For example, it relies on COM interop or calls methods in a 32-bit dynamic link library. ... To address this exception, set the project's Platform target property to x86 (instead of x64 or AnyCPU) and recompile.

这篇关于带有NHibernate的System.Data.SQLite BadImageFormatException,在独立时可以正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆