开发者

SQLite NHibernate configuration with .Net 4.0 and vs 2010

I am updating this post with what I think I now know about getting this configuration; HOWEVER, there is more to know as I am still having a problem is one crucial area.

I use SQLite for unit testing, which now works fine, using the configuration steps below. I also use it when I want a test run of the UI with more data than in-memory test data but without the overhead of SQLServer - this configuration fails with the following:

{"Could not create the driver from NHibernate.Driver.SQLite20Driver, NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4."}

Here is updated info on configs that DO work:

1) Which SQLite dll?? There are some bad links out there that look helpful but that have build errors in them. The only good download as of this date is here at Source Forge. v1.066 which was released today, 4-18-2010.

2) Must you use the GAC? No, as answered by Mauricio.

3) x64 builds - as answered by Mauricio.

4) NHib driver - SQLite20Driver, as answered by Mauricio

5) FNH as a potential conflict - no, as answered by Mauricio

Cheers,

Berryl

== ADD'L DEBUG INFO ===

When the exception is hit and I call up the SQLite20Drive assembly, I get the following which suggests to me that the driver should be available. I am wondering though, as the configuration code is in a different assembly.

-- assembly when error ----

    ?typeof(SQLite20Driver).Assembly
{NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4}
[System.Reflection.RuntimeAssembly]: {NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4}
CodeBase: "file:///C:/Users/Lord & Master/Documents/Projects/Smack/trunk/src/ConstructionAdmin.WpfPresentation/bin/Debug/NHibernate.DLL"
EntryPoint: null
EscapedCodeBase: "file:///C:/Users/Lord%20%26%20Master/Documents/Projects/Smack/trunk/src/ConstructionAdmin.WpfPresentation/bin/Debug/NHibernate.DLL"
Evidence: {System.Security.Policy.Evidence}
FullName: "NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4"
GlobalAssemblyCache: false
HostContext: 0
ImageRuntimeVersion: "v2.0.50727"
IsDynamic: false
IsFullyTrusted: true
Location: "C:\\Users\\Lord & Master\\Documents\\Projects\\Smack\\trunk\\s开发者_JAVA技巧rc\\ConstructionAdmin.WpfPresentation\\bin\\Debug\\NHibernate.dll"
ManifestModule: {NHibernate.dll}
PermissionSet: {<PermissionSet class="System.Security.PermissionSet"
version="1"
Unrestricted="true"/>
}
ReflectionOnly: false
SecurityRuleSet: Level1

--- assembly when unit testing (NO ERROR)

{NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4}
[System.Reflection.RuntimeAssembly]: {NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4}
CodeBase: "file:///C:/Users/Lord & Master/Documents/Projects/Smack/trunk/src/ConstructionAdmin.Tests/bin/Debug/NHibernate.DLL"
EntryPoint: null
EscapedCodeBase: "file:///C:/Users/Lord%20%26%20Master/Documents/Projects/Smack/trunk/src/ConstructionAdmin.Tests/bin/Debug/NHibernate.DLL"
Evidence: {System.Security.Policy.Evidence}
FullName: "NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4"
GlobalAssemblyCache: false
HostContext: 0
ImageRuntimeVersion: "v2.0.50727"
IsDynamic: false
IsFullyTrusted: true
Location: "C:\\Users\\Lord & Master\\Documents\\Projects\\Smack\\trunk\\src\\ConstructionAdmin.Tests\\bin\\Debug\\NHibernate.dll"
ManifestModule: {NHibernate.dll}
PermissionSet: {<PermissionSet class="System.Security.PermissionSet"

version="1" Unrestricted="true"/> } ReflectionOnly: false SecurityRuleSet: Level1

Here is the bootstrapper for this a SQLite session:

    /// <summary>SQLite-NHibernate bootstrapper for general use.</summary>
public class SQLiteBoot : IDisposable
{
    public readonly ISessionFactory SessionFactory;
    private readonly ISession _session;
    private static Configuration _config;
    private static string _persistenceModelGeneratorName;

    public SQLiteBoot(IAutoPersistenceModelGenerator persistenceModelGenerator) {
        if (_isSessionFactoryBuildRequired(persistenceModelGenerator)) {
            _config = new Configuration()
                .SetProperty(ENV.ReleaseConnections, "on_close")
                .SetProperty(ENV.Dialect, typeof (SQLiteDialect).AssemblyQualifiedName)
                .SetProperty(ENV.ConnectionDriver, typeof (SQLite20Driver).AssemblyQualifiedName)
                .SetProperty(ENV.ConnectionString, "data source=:memory:")
                .SetProperty(ENV.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName)
                .SetProperty(ENV.CurrentSessionContextClass, typeof (ThreadStaticSessionContext).AssemblyQualifiedName);

            _persistenceModelGeneratorName = persistenceModelGenerator.Name;
            var persistenceModel = persistenceModelGenerator.Generate();
            var fluentCfg = Fluently.Configure(_config).Mappings(m => m.AutoMappings.Add(persistenceModel));
            SessionFactory = fluentCfg.BuildSessionFactory();
            Check.Require(SessionFactory.GetAllClassMetadata().Count > 0, "No mapped classes - check your AutoPersistenceModel!");

        }

        _session = SessionFactory.OpenSession();
        CurrentSessionContext.Bind(_session);

        new SchemaExport(_config).Execute(true, true, false, _session.Connection, Console.Out);
    }

    private bool _isSessionFactoryBuildRequired(IAutoPersistenceModelGenerator persistenceModelGenerator)
    {
        return
            _config == null
            || SessionFactory == null
            || !persistenceModelGenerator.Name.Equals(_persistenceModelGeneratorName);
    }

    public void Dispose()
    {
        _session.Dispose();
    }
}

}


  1. Sure. You can also use previous versions if you configure mixed mode loading.
  2. No need to be in the GAC. You can use gacutil to remove the assemblies from the GAC.
  3. Use the x64 DLL to target Windows x64 and x86 for Windows x86
  4. Please post the full exception stack trace. Also if you're using a 3.5 assembly use mixed mode loading.
  5. FNH has no reference to SQLite.


I want this to stand out so it will help someone else; the full reason this happens is explained here; so adjust your congig to use BOTH the redirect there in combo with the mixed loading mode referenced here by Mauricio.


I had the same problem, and found little or no help on all the forum and blog posts.

Note that this problem is specific to a case respecting all of the following criteria: - using SQLite - with System.Data.SqlLite - on an x64 machine - and NHibernate (2.1.2.4 in my case)

That chunk of config in my web.config (or app.config for my unit tests) got it to work. I had to qualify the assembly to be sure he loads correctly.

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

Somewhere in it's inner plumbing, during the mapping using scanned assemblies, NHibernate creates an Assembly object using it's paritla name, as a string, "System.Data.SQLite". Somehow, the x86 version of the assembly got loaded.

The above configuration made sure that using the partial name to load an assembly would provide the x64 version.

EDIT: I use version 1.0.66.0 and took the DLL under the bin\x64 folder in the file SQLite-1.0.66.0-binaries.zip available on sourceforge here.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜