In one of my NH ClassMaps, I map a VARBINARY(MAX) column like this
Property(x => x.OriginalImage, map => { map.Column("OriginalImage"); map.NotNullable(true); map.Lazy(true); });
but when I save an uploaded image, the binary data in the database is truncated to 8000 bytes.
Adding a
map.Length(int.MaxValue);
causes NHibernate to expect an IMAGE column rather than VARBINARY(MAX) (using the MsSql2005Dialect dialect).
After a short search I found two solutions for this problem:
- Explicitly define a SqlType():
Property(x => x.OriginalImage, map =>
{
map.Column(c => { c.Name(“OriginalImage”); c.SqlType(“VARBINARY(MAX)”); });
map.NotNullable(true);
map.Lazy(true);
map.Length(int.MaxValue);
});
based on this SO answer referring to Fluent rather than Loquacious.
- Subclass the dialect
As illustrated in this SO answer
public class MyMsSql2008Dialect : MsSql2008Dialect { protected override void RegisterLargeObjectTypeMappings() { base.RegisterLargeObjectTypeMappings(); base.RegisterColumnType(DbType.Binary, 2147483647, "VARBINARY(MAX)"); } }