Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NHibernate and Mono.Data.SQLite

I read and implemented Trying to using Nhibernate with Mono & SQLite - can't find System.Data.SQLite However, as the last comment there states this seems not to work with NHibernate 3.1

The error is

HibernateException: The IDbCommand and IDbConnection implementation in the assembly Mono.Data.Sqlite could not be found. Ensure that the assembly Mono.Data.Sqlite is [...reachable...]

I have Mono.Data.Sqlite in the GAC.
I have tried both specifying "Mono.Data.Sqlite" as well as typeof(Mono.Data.Sqlite.SqliteConnection).Assembly.FullName as the name of the assembly

Has anyone any Ideas how to get this working?

like image 758
Nils Avatar asked Oct 02 '11 12:10

Nils


1 Answers

There is a problem in the answer of Trying to using Nhibernate with Mono & SQLite - can't find System.Data.SQLite . For the given constructor (3 parameters) to work the assembly in question (Mono.Data.Sqlite) needs to be loaded first.

This works if the 4-parameter base contructor is used like this:

public class MonoSQLiteDriver : NHibernate.Driver.ReflectionBasedDriver  
{  
        public MonoSQLiteDriver() 
            : base(
            "Mono.Data.Sqlite",
            "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;  
        }  
    }  
}  

(Still, credit goes to http://intellect.dk/post/Why-I-love-frameworks-with-lots-of-extension-points.aspx for the original idea - thanks.)

And if you use FluentNHibernate, then you'll also need:

public class MonoSQLiteConfiguration : PersistenceConfiguration<MonoSQLiteConfiguration>
{
    public static MonoSQLiteConfiguration Standard
    {
        get { return new MonoSQLiteConfiguration(); }
    }

    public MonoSQLiteConfiguration()
    {
        Driver<MonoSQLiteDriver>();
        Dialect<SQLiteDialect>();
        Raw("query.substitutions", "true=1;false=0");
    }

    public MonoSQLiteConfiguration InMemory()
    {
        Raw("connection.release_mode", "on_close");
        return ConnectionString(c => c
            .Is("Data Source=:memory:;Version=3;New=True;"));

    }

    public MonoSQLiteConfiguration UsingFile(string fileName)
    {
        return ConnectionString(c => c
            .Is(string.Format("Data Source={0};Version=3;New=True;", fileName)));
    }

    public MonoSQLiteConfiguration UsingFileWithPassword(string fileName, string password)
    {
        return ConnectionString(c => c
            .Is(string.Format("Data Source={0};Version=3;New=True;Password={1};", fileName, password)));
    }
}

I have not encountered any problems so far...

like image 116
Nils Avatar answered Sep 23 '22 07:09

Nils