Trying to using Nhibernate with Mono & SQLite - ca

2019-03-27 16:29发布

I wrote a simple app in mono (C#) that uses NHibernate with MYSQL - and I now want to port it to SQLite.

My hope is (was) that I could simply change hibernate.cfg.xml and point it to a different database. Here is my modified hibernate.cfg.xml:

    <?xml version="1.0" encoding="utf-8" ?>

    <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
        <session-factory name="NHibernate.Test">
            <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
            <property name="connection.connection_string">
                Data Source=nhibernate_test.db;Version=3
            </property>
            <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
            <property name="query.substitutions">true=1;false=0</property>
            <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
        </session-factory>

</hibernate-configuration> 

The problem is that I'm getting an error to the effect that it can't find System.Data.SQLite. This doesn't surprise me since, as I understand it, in mono we should be using Mono.Data.SQLite.

The trouble is (assuming I'm understanding the problem correctly) I don't know how to tell NHibernate to use Mono.Data.SQLite instead of System.Data.SQLite.

This is all being done on Linux - if that makes any difference.

Does anyone have any ideas how to proceed?

2条回答
贪生不怕死
2楼-- · 2019-03-27 17:05

You need to make nHibernate aware of the Mono.Data.SQLite assembly. Add this to the configuration:

<add key="connection.driver_class" value="Name.Space.MonoSqliteDriver, AssemblyName" />

And you also need a simple MonoSQLiteDriver class:

public class MonoSqliteDriver : NHibernate.Driver.ReflectionBasedDriver  
{  
    public MonoSqliteDriver() :   
        base("Mono.Data.Sqlite",  
        "Mono.Data.Sqlite.SqliteConnection",  
        "Mono.Data.Sqlite.SqliteCommand")  
    {  
    }  
    public override bool UseNamedPrefixInParameter {  
        get {  
            return true;  
        }  
    }  
    public override bool UseNamedPrefixInSql {  
        get {  
            return true;  
        }  
    }  
    public override string NamedPrefix {  
        get {  
            return "@";  
        }  
    }  
    public override bool SupportsMultipleOpenReaders {  
        get {  
            return false;  
        }  
    }  
}  

(code taken from http://intellect.dk/post/Why-I-love-frameworks-with-lots-of-extension-points.aspx )

查看更多
ら.Afraid
3楼-- · 2019-03-27 17:06

Note that you might need to use 4 parameters instead of 3 for the call to the constructor of the ReflectionBasedDriver base class, see Using NHibernate and Mono.Data.Sqlite

查看更多
登录 后发表回答