问题
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).
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
I debugged a bit NHibernate and the Exception is thrown at the folllowing statement
System.Type.GetType("System.Data.SQLite.SQLiteConnection, System.Data.SQLite");
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?
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>
回答1:
If VS express does not allow you to choose a platform you can try setting it up manually in .csproj file
<PlatformTarget>x86</PlatformTarget>
After building your app make sure that:
- 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
Also try removing useLegacyV2RuntimeActivationPolicy
from config.
From BadImageFormatException doc:
... 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.
来源:https://stackoverflow.com/questions/7219202/system-data-sqlite-badimageformatexception-with-nhibernate-works-finewhen-stand