NHibernate How to specify custom sql type only in production

I am storing binaries in Sql Server 2005 Db using Fluent NHibernate. However, I am using SQLite to run my (pseudo) unit tests.

I need to use native Sql type for Ms Sql, but this will throw an error in SqlLite. What strategies can I use?

This is the map file:

public class BinaryFile
 {   
 public BinaryFile()
    {         
        m.Map(x => x.BinaryData);//.CustomSqlType("varbinary(MAX)");
        m.Map(x => x.ContentType);
        m.Map(x => x.FileName);
        m.Map(x => x.FileSize);
    }
 }

      

+2


a source to share


2 answers


I'm not familiar with the current nhibernate, but one way you could use would be using conditional compilation, so when you are in debug mode it displays a single value and when in release it displays a production value.



 #if DEBUG          
          m.Map(x => x.BinaryData);
 #else
        //Map production here           
 #endif

      

+1


a source


Could you use a different ClassMap for each configuration? You probably have to explicitly add each ClassMap to your Fluent session configuration, which would make it more verbose, but that would mean you can use a different mapping class for different databases.

public class BinaryFileMSSqlServer
{   
    public BinaryFile()
    {         
        m.Map(x => x.BinaryData).CustomSqlType("varbinary(MAX)");
        m.Map(x => x.ContentType);
        m.Map(x => x.FileName);
        m.Map(x => x.FileSize);
    }
}

public class BinaryFileSQLite
{   
    public BinaryFile()
    {         
        m.Map(x => x.BinaryData);
        m.Map(x => x.ContentType);
        m.Map(x => x.FileName);
        m.Map(x => x.FileSize);
    }
}

      

Your current session mapping will look something like this:



Fluently.Configure()
  .Database(MsSqlConfiguration.MsSql2005
    .ConnectionString(c => c
      .FromAppSetting("connectionString"))
    .Cache(c => c
      .UseQueryCache()
      .ProviderClass<HashtableCacheProvider>())
    .ShowSql())
  .Mappings(m => m.FluentMappings
      .Add<BinaryFileMSSqlServer>()
      .Add<...>()
      .Add<...>())
  .BuildSessionFactory();

      

You will need to fill in each of your mapping classes manually. You will also need to create a separate free configuration for SQLite, using special Classite SQLite classes as needed.

+1


a source







All Articles