Problems using FluentNHibernate + SQLite with .NET4?

前端 未结 7 861
离开以前
离开以前 2020-12-14 22:17

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 thi

相关标签:
7条回答
  • 2020-12-14 22:59

    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.

    0 讨论(0)
  • 2020-12-14 23:00

    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>
    
    0 讨论(0)
  • 2020-12-14 23:02

    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 讨论(0)
  • 2020-12-14 23:08

    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.

    0 讨论(0)
  • 2020-12-14 23:11

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

    0 讨论(0)
  • 2020-12-14 23:16

    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

    0 讨论(0)
提交回复
热议问题