views:

45

answers:

2

I am saving binary files into a Sql Server 2005 Db using Fluent NHibernate. However, I am using SQLite to run my (pseudo) Unit Tests.

I need to use a custom Sql type for Ms Sql, but it would throw an error on 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);
    }
 }
+1  A: 

I am not familiar with fluent nhibernate, but one way you could use would be to use conditional compilation, so when you are in debug mode it maps one value, and when in release it maps the production value.

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

Could you use different ClassMap classes for each config? You would probably have to explicitly add each ClassMap to your Fluent session configuration, which would make it more verbose, but it would mean you can use a different mapping class for the 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 fluent session mapping would then 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 would need to fill in each of your mapping classes manually. You would also need to create a separate fluent configuration for SQLite, using the SQLite specific ClassMaps where necessary.

Jamie Penney