NHibernate 3.0 binary data truncation

February 01, 2011 - fluentnhibernate nhibernate

NHibernate 3.0.0 has a bug which silently truncates data going into varbinary(MAX) fields to 8000 bytes — not a pleasant one to find in production because your test documents weren't big enough! This stackoverflow answer contains a workaround for hbm file mappings; translated to FluentNHibernate auto mapping it becomes:

var sessionFactory = Fluently.Configure() 
  .Database(/\* database config \*/)
  .Mappings(m => m.AutoMappings
  .Add(AutoMap.AssemblyOf<Document>()
  .Override<Document>(map => map.Map(d => d.Data).Length(2147483647))))
  .BuildSessionFactory();

The bug is apparently fixed in 3.0.1.