开发者

Problems using FluentNHibernate + SQLite with .NET4?

开发者 https://www.devze.com 2022-12-27 17:50 出处:网络
I have a WPF application running with VS2010 .Net3.5 using Nhibernate with FluentNHibernate + SQLite, and all works fine.

I have a WPF application running with VS2010 .Net3.5 using Nhibernate with FluentNHibernate + SQLite, and all works fine.

Now I want to change to use .Net4, but this has turned into a more painful experience then I expected.. When setting up the connection I do this:

var cfg = Fluently.Configure().
    Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("MyDb.db")).
    Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>());
_sessionFactory = cfg.BuildSessionFactory();                    

The BuildSessionFactory() call throws a FluentConfigurationException saying:

An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more details.

The inner exception gives us more information:

Could not create the driver from N开发者_StackOverflowHibernate.Driver.SQLite20Driver, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.

And further InnerException:

The IDbCommand and IDbConnection implementation in the assembly System.Data.SQLite could not be found. Ensure that the assembly System.Data.SQLite is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use element in the application configuration file to specify the full name of the assembly.

Now - to me it sounds like it doesn't find System.Data.SQLite.dll, but I can't understand this. Everywhere this is referenced I have "Copy Local", and I have verified that it is in every build folder for projects using SQLite. I have also copied it manually to every Debug folder of the solution - without luck.

Notes:

  • This is exactly the same code that worked just fine before I upgraded to .Net4.
  • I did see some x64 x86 mismatch problems earlier, but I have switched to use x86 as the target platform and for all referenced dlls. I have verified that all files in the Debug-folder are x86.
  • I have tried the precompiled Fluent dlls, I have tried compiling myself, and I have compiled my own version of Fluent using .Net4.
  • I see that there are also others that have seen this problem, but I haven't really seen any solution yet.

After @devio's answer I tried adding a reference to the SQLite dll. This didn't change anything, but I hope I made it right though.. This is what I added to the root node of the app.config file:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <qualifyAssembly partialName="System.Data.SQLite" fullName="System.Data.SQLite, Version=1.0.60.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </assemblyBinding>
</runtime>

Anyone out there using Fluent with .Net4 and SQLite successfully? Help! I'm lost...


I also got the same error message when I tried Fluent with .Net4 and SQLite, but when I looked more closely, I found different error message.

Could not load type System.Data.SQLite.SQLiteConnection, System.Data.SQLite. System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

So what I did is to add useLegacyV2RuntimeActivationPolicy="true" to the "startup" tag like this.

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>

I don't need to add dependentAssembly or anything inside the "runtime" tag. According to this link text and this link text, it should be used for migration aid only. So hopefully, SQLite will be updated soon.

Hope this helps! Karlkim


Check the version of your System.Data reference. It sounds to me like System.Data.SqlLite can't find the version of IDbCommand and IDbConnection that it was built with which I suspect is version 2.0.0.0. I suspect that now you've upgraded to .Net 4 you are referencing System.Data version 4.0.0.0.

If this is the case you should be able to add a binding redirect to resolve the problem:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="System.Data" publicKeyToken="b77a5c561934e089"/>
        <bindingRedirect oldVersion="2.0.0.0" newVersion="4.0.0.0"/>
    </dependentAssembly>
</assemblyBinding>


I had a similar problem with NH, VS2010, .Net4, but with the Oracle ODP.Net drivers and 32bit.

The solution was to declare a "qualified assembly" in the web.config file with an explicit version number. See my summary.

Maybe this solution applies to your problem as well.


I managed to resolve it by changing the target platform to x86 in project debug setting. I am using vs2010.


Yes sir: Got it to work with .NET 4 - Fluent NHibernate - NUnit

By using the information from the above posts I managed to get it working under Visual Studio 2010 and in my NUnit library for testing, compiled for Any CPU in debug mode.

First I kept getting the same exception as some of you experience:

Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=3.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4.

I've used NUGet to download the 1.0.76.0 version of SQLite for v4.0.30319 of .NET. Devios post above and his summary link got me into testing assemblyBinding in my App.config file - telling VS 2010 to use the correct version of System.Data.SQLite for NHibernate. Here's what I put in my App.Config:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <qualifyAssembly partialName="System.Data.SQLite" 
      fullName="System.Data.SQLite, Version=1.0.76.0, 
      Culture=neutral, 
      PublicKeyToken=db937bc2d44ff139"/>
    </assemblyBinding>
  </runtime>

How I knew which details to put in the App.config ?

I've also previously installed the statically linked library of SQLite's .NET 4 version from the SQLite website under their download section. This installed the SQLite library to my computer and added it to the Global Assembly Cache - in turn giving me the information I needed to edit the App.config using the Visual Studio Command Prompt 2010:

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>gacutil -l System.Data.SQLite Microsoft (R) .NET Global Assembly Cache Utility. Version 4.0.30319.1 Copyright (c) Microsoft Corporation. All rights reserved.

The Global Assembly Cache contains the following assemblies:
System.Data.SQLite, Version=1.0.76.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64

Number of items = 1

Usually I would use .NET Reflector from RedGate for extracting the information of dll's of my previously NUget added System.Data.SQLite dll file, but the gacutil -l System.Data.SQLite is ok if you've already installed SQLite to your system, the correct version that is.

Previously I also got the mixed mode assembly exception:

Could not load type System.Data.SQLite.SQLiteConnection, System.Data.SQLite. System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

Then if I wanted to use the .NET 2.0 version of SQLIte I'd insert the following to my App.config:

 <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

But now that I have a .NET 4 version of SQLite I do not need to run the old .NET 2 version and in legacy mode - avoiding potential conflicts further on using In-Process Side-by-Side Runtime Host Activation - Google MSDN:

When a newer version of the .NET Framework CLR loaded an assembly that was built against a previous version of the CLR, compatibility issues could occur, and the application could stop working. This could happen for any managed assembly, both in full applications and in plug-ins (where the managed assemblies run in the context of a host application). The only way to guarantee that a newer version of the .NET Framework did not affect existing applications was to have all existing managed applications run in their original target compiled .NET Framework version.

Thanks to all you guys for pointing me in the right direction ! Hopefully it will be easier for others after us to fix this issue from now on.


Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4

Solution:

Test settings:
choose hosts
choose run tests in 64 bit process on 64 bit machine


I tried every solution on this page and nothing worked. Then I uninstalled my 64bit version and used the x86 version from Nuget and everything worked.

PM> Install-Package System.Data.SQLite.x86

0

精彩评论

暂无评论...
验证码 换一张
取 消