Mono下配置ActiceRecord使用sqlite数据库

    .Net下我们经常使用的System.Data.Sqlite是一个嵌入了原生c++ sqlite数据库模块的程序集,由于大多数的Linux下默认已经具备了Sqlite的运行环境,System.Data.Sqlite无法运行在Linux下。所以,Mono.Data.Sqlite [1]出现了,Mono.Data.Sqlite可以让Mono下的程序使用相同的类名操作sqlite数据库,方便应用程序从Windows迁移到Linux:你只需更改项目引用即可。

    但由于ActiceRecord已封装了NHibernate的数据库驱动类"NHibernate.Driver.SQLite20Driver",而该驱动类默认从System.Data.Sqlite程序集中加载所需类,因此ActiceRecord使用Sqlite在Linux环境下的Mono中运行就出现了矛盾:ActiveRecord需要System.Data.Sqlite,而该程序集无法在Linux环境下的Mono中被正常运行,这就需要一个中间类[2]来进行转接,并且进行对应的配置。

 

1. 在项目中添加中间类MonoSqliteDriver

namespace Your.Assembly
{
    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;
            }
        }
    }
}

 

2. 修改ActiveRecord配置文件

<add key="connection.driver_class" value="NHibernate.Driver.SQLite20Driver" />
<!--<add key="connection.driver_class" value="Your.Assembly.MonoSqliteDriver,Your.Assembly" />-->

 

3. 添加Mono.Data.Sqlite引用

以Ubuntu 10.04为例,Mono.Data.Sqlite.dll可以从该目录下找到

cp /usr/local/lib/mono/4.0/Mono.Data.Sqlite.dll ~/

 

参考:

[1]. Mono sqlite introduction http://www.mono-project.com/SQLite

[2]. Trying to using Nhibernate with Mono & SQLite http://stackoverflow.com/questions/6203597/trying-to-using-nhibernate-with-mono-sqlite-cant-find-system-data-sqlite

posted @ 2011-11-20 21:39  傲雪啸风  阅读(1900)  评论(0编辑  收藏  举报