Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using FluentNHibernate + SQLite with .NET4?

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 NHibernate.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...

like image 230
stiank81 Avatar asked Apr 23 '10 10:04

stiank81


2 Answers

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

like image 122
kimsk Avatar answered Oct 06 '22 03:10

kimsk


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>
like image 36
s1mm0t Avatar answered Oct 06 '22 03:10

s1mm0t