Fixing the Blob Truncation problem with NHibernate 3.2.0 GA

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)");
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: