views:

931

answers:

4

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

+2  A: 

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
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
web.config is the name for web applications, app.config is the name for every other type of app. So: yes!
devio
Tried adding this without luck.. See edit in the question for details on what I added.
stiank81
+3  A: 

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
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 <runtime> tag? Tried both.. What can be wrong? oldVersion? How can I find the correct version?
stiank81
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
Should SQLite work seemlessly with the latest version of System.Data anyway?
stiank81
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
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
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
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
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.zipNow that 2010 is RTM, based on comments by Robert Simpson in that thread, there should be a 4.0 build out RSN :)
James Manning
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
From those links, you're right it does look as though .Net 4 isn't supported.
s1mm0t
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
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
Deleted my new question - nevermind..
stiank81
+3  A: 

Hi stiank81, 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

kimsk
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
No problem. I didn't realize that there were more comment :-). Anyway, the issue has been resolved!
kimsk
A: 

Hi, I came across the same issue. I tried all of the above to no avail. However, I managed to resolve it by changing the target platform to x86 in project debug setting. I am using vs2010.

Hope this helps.

mackah666

mackah666