12

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

stiank81
  • 25,418
  • 43
  • 131
  • 202
  • Any possibility that it is a problem with 2.x SQLite vs 3.x? – AJ. Apr 24 '10 at 06:09
  • Don't know.. You think? Why would it work with .Net3.5 then? Will try the latest SQLite version soon.. – stiank81 Apr 24 '10 at 06:50
  • Have you also recently upgraded to later version of NHibernate? I have had similar problems with 2.1.2.4000 (but MySQL in that case). – UpTheCreek Apr 27 '10 at 08:15
  • No - been using the same version of NHibernate all the way. All working fine with .NET3.5. Then I change to use .NET4, and the problem shows. – stiank81 Apr 27 '10 at 08:27

7 Answers7

8

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

Community
  • 1
  • 1
kimsk
  • 2,221
  • 22
  • 23
  • Thanks. Actually that same solution is given in one of the many comments of the accepted answer, and was what solved it for me. Good to have it as a separate answer though. +1 – stiank81 May 04 '10 at 07:03
  • No problem. I didn't realize that there were more comment :-). Anyway, the issue has been resolved! – kimsk May 04 '10 at 14:14
7

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>
s1mm0t
  • 6,035
  • 4
  • 38
  • 45
  • 1
    This sounds promissing! You are absolute right that I now reference version System.Data version 4.0.0.0 (runtime version v4.0.30319). I tried adding the assembly-binding to app.config without result though. Should it be within the tag? Tried both.. What can be wrong? oldVersion? How can I find the correct version? – stiank81 Apr 27 '10 at 20:21
  • Shouldn't System.Data.SQLite.dll come with the System.Data version it needs - embedded? Or will it still be confused because there is a version available in the application folder, but of the wrong version? – stiank81 Apr 27 '10 at 21:26
  • Should SQLite work seemlessly with the latest version of System.Data anyway? – stiank81 Apr 27 '10 at 21:39
  • From what you've said in your comments, I'm sure this is the cause of your problem. Yes, the assembly binding config should go within the runtime tag. To check what versions of System.Data you've got installed, start a visual studio command prompt and run the command: gacutil -l System.Data. I am not running the latest release of VS 2010 so there maybe a chance that the the version number has changed from 4.0.0.0 to something else. Failing that, the only other thing I can think of is deleting your reference to the 4x version of System.Data and reference the 2.x version instead... – s1mm0t Apr 28 '10 at 02:28
  • As far as I know, System.Data.SQLite.dll will not have System.Data embedded and will try to reference the version it was built against, from the gac. I don't know for sure whether SQLite will work seamlessly with the latest version of System.Data but I would expect to be able to get past this problem. – s1mm0t Apr 28 '10 at 02:32
  • Thanks for following up. I'm running the latest VS2010, and installed versions of System.Data are 2.0.0.0 and 4.0.0.0. Should setting the assemblyBinding in app.config also affect referenced dlls? Like System.Data.SQLite.dll in this case. I guess you're right about System.Data not being embedded in SQLite, but as shouldn't it be fine as long as v2 is in the GAC? Reference version 2 of System.Data wouldn't be ideal.. – stiank81 Apr 28 '10 at 07:50
  • Obviously System.Data.SQLite 1.0.66.0 doesn't support .Net4: http://sqlite.phxsoftware.com/forums/p/2323/9279.aspx#9279. Some interesting notes in this thread: http://sqlite.phxsoftware.com/forums/t/2040.aspx – stiank81 Apr 28 '10 at 08:01
  • make sure to use something (fuslogvw, Debug -> Windows -> Modules, whatever) to see which version(s) of System.Data are getting loaded in your app. The fusion log also details the steps used, including any binding redirects that take effect. That said, IMHO just save your headaches and get (or create) a 4.0 build of the provider instead. The one mentioned in the thread linked by stiank81 is @ http://sqlite.phxsoftware.com/forums/storage/29/9252/SQLite-1.0.66.1-vs2010-net4.zip Now that 2010 is RTM, based on comments by Robert Simpson in that thread, there should be a 4.0 build out RSN :) – James Manning Apr 28 '10 at 10:53
  • Loaded version of System.Data is 4.0.30319.1. In the reference it is marked with version 4.0.0.0 and 4.0.30319 as "runtime version". I tried using the SQLite version built for .Net4 without luck.. Maybe something else is wrong (too) ?? Hoping for an official release supporting .Net4 soon! – stiank81 Apr 28 '10 at 11:22
  • From those links, you're right it does look as though .Net 4 isn't supported. – s1mm0t Apr 28 '10 at 13:25
  • Actually it seems like I made it work after all adding the app.config setting suggested here: http://sqlite.phxsoftware.com/forums/t/2322.aspx But I got some new issues when I got passed this, so I'm gonna have to keep digging. Sigh... – stiank81 Apr 28 '10 at 20:08
  • The bounty points goes to you s1mm0t. You led the way to the answer that helped me. I'm having problems with the style now though.. If you know anything about this please check out my new question here: http://stackoverflow.com/questions/2736044/wpf-global-style-definitions-with-net4 – stiank81 Apr 29 '10 at 09:18
2

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.

devio
  • 36,858
  • 7
  • 80
  • 143
  • Thanks, but I don't have a web.config as this is a wpf-app. Can I add it to the app.config instead? – stiank81 Apr 27 '10 at 09:05
  • web.config is the name for web applications, app.config is the name for every other type of app. So: yes! – devio Apr 27 '10 at 09:57
  • Tried adding this without luck.. See edit in the question for details on what I added. – stiank81 Apr 27 '10 at 11:43
2

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

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
mackah666
  • 21
  • 1
1

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.

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136
nori
  • 11
  • 2
0

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

tkunstek
  • 26
  • 2
0

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

Yi Jiang
  • 49,435
  • 16
  • 136
  • 136