4
votes

I'm working on the project using Nhibernate 3.0 and Fluent Nhibernate. We are using SQL-Server 2008 for production purposes. For versioning we rely on SQL-Server side generated timestamps.

Recently I've started writing repository tests using in memory SQLite database. Unfortunately SQLite cannot generate timestamps, so any insert fails with constraint violation.

I want to change Version mapping to Nhibernate managed one, when I compile my mappings for SQLite underlying database. The code conveying this idea would look roughly like this:

public class CommonClassMap<T> : ClassMap<T> where T:Entity
{
    public CommonClassMap()
    {
        if (SQLITE)
        {
            Version(n => n.Version);
        }
        else
        {
            Version(n => n.Version).CustomSqlType("timestamp").UnsavedValue("null").CustomType("BinaryBlob").
                Generated.Always();
        }

Unfortunately, I was not able to find any way to change mappings at the run-time or read underlying database information from the ClassMap. Any help is welcome.

1

1 Answers

0
votes

What I do is to have separate ClassMaps for different DB providers. I however do not have a requirement of being able to change this in runtime, it is configured once for the whole lifetime of the application.

You can still keep your system, but I guess you'll have keep the SQLITE and similar stuff yourself. Also do not rely too much that code which runs on one DB provider (like SQLite) will run on other providers too, so you should always include tests for SQL Server, too.