23

I have upgraded from Fluent Nhibernate 1.0 with Nhibernate 2.1 to pre- release 1.x with NHibernate 3.0 GA and have hit what I think is a regression, but I want to hear if that's indeed the case.

I am using SQL Server Express 2008 and the MSSQL 2008 dialect and have an Image property of type System.Drawing.Image and I have mapped it like this:

Map (food => food.Image)
 .Length (int.MaxValue)
 .Nullable ();

The Image column in the table is of type varbinary(MAX).

The generated hbm for the property is:

<property name="Image" type="System.Drawing.Image, System.Drawing,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
   <column name="Image" length="2147483647" not-null="false" />
</property>`

However no matter what I do the binary blob is truncated to 8000 bytes when serialized with the current FNH and NH versions. That didn't used to be the case with previous versions.

Ideas of why this is happening and how to fix/workaround it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50

5 Answers5

22

I too have encountered a similar problem and after much experimentation I noticed that when using Nhibernate to generate my schema to a file the generated column type was always length 8000.

Setting setting CustomSqlType to Varbinary(max) as suggested above made no difference, however, this work around in my FluentMapping seemed to do the trick:

Map(x => x.LogoBytes).CustomType("BinaryBlob").Length(1048576).Nullable();  

The length of course is an arbitrary amount but I think it should be set to something less than int.Max. I am new to Nhibernate so I'm still figuring things out but I'd be interested to know if this helps you.

sbeskur
  • 2,260
  • 23
  • 23
  • 4
    This corrected it for me; however, I just used int.MaxValue for the Length so as not to impose any low limits. – Steve Scheffler Mar 21 '11 at 17:38
  • As of nhibernate 3.2.0.4000 and fluentnhibernate 1.3.0.717 i still had to use this to get past the 8000 limit. – chrisortman Feb 04 '12 at 06:07
  • The solution worked for me too. As I'm using NH 3.2.400 and Mapping by code, I used following: map.Property(x => x.Image, status => status.Column(c => { c.SqlType("VARBINARY(MAX)"); c.Length(int.MaxValue); })); – Afshar Mohebi Jul 05 '12 at 11:18
  • 2
    I've found simply setting length to `int.MaxValue` is enough to solve this issue. – Samuel Parkinson Jan 07 '13 at 15:39
  • The [issue reported in NHibernate JIRA](https://nhibernate.jira.com/browse/NH-2764) indicates that they won't fix this, but you rather need to specify the length manually, so this is the correct approach to the problem. – Alpha Feb 28 '13 at 13:49
5

In 3.0.0GA, the following mapping seems to do the trick:

        <property name="Data" type="Serializable" length="2147483647" />
Michael Teper
  • 4,591
  • 2
  • 32
  • 49
4

This is a regression. I have raised a bug and provided patches at https://nhibernate.jira.com/browse/NH-2484

Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
  • I have the same problem with NHibernate 3.1.0.4000. Am I doing something wrong? – labilbe Apr 07 '11 at 18:11
  • I don't know - I use a patched 3.0 and can't update to 3.1 to test right now due to other deps on 3.0. Easiest would be to take my test case from the bug report, replace 3.0 with 3.1 and see if it still throws. If it does we should reopen the defect and temporally you can use a custom UserType for Image which forces the size of the data. – Ivan Zlatev Apr 07 '11 at 20:14
  • After more testing, I found NH 3.1.0 is not buggy anymore. I was using corrupted data (saved with 8000 bytes) of another version, then I had difficulties to find out the bug cause. It's ok now. Thank you! – labilbe Apr 08 '11 at 09:10
1

Map(x => x.Image).Length(100000).Not.Nullable();

Add the 'Length(MAXVALUE)' as above and it will work :)

Kev
  • 743
  • 2
  • 14
  • 32
0

Have you tried this?

Map(x => x.Image).CustomSqlType("VARBINARY(MAX)");
Rippo
  • 22,117
  • 14
  • 78
  • 117
  • Yes I have tried it and it makes no difference. I have checked with the Fluent NHibernate guys and it's not a regression on their side so I have raised an nhibernate issue. – Ivan Zlatev Jan 03 '11 at 22:51