1

I looked through all the answers here and elsewhere on the web, but nothing seems to work. I have a table (sql server 2014) with a varbinary(max) datatype, named "Image" I'm using NHibernate version 3.3.1.4000, which has some fixes regarding varbinary issues.

this is my current hibernate mapping:

   <property name="Image" type="BinaryBlob">
      <column name="Image" sql-type="varbinary(2147483647)" length="2147483647"/>                                             
   </property>

I also tried following variations:

<property name="Image" type="BinaryBlob"/>

<property name="Image"  length="2147483647"/>

this is my (c# class) property:

   public virtual byte[] Image { get; set; }

I'm getting the familiar error:

 ---> System.Data.SqlClient.SqlException: String or binary data would be truncated.

I have a feeling the answer is staring me right in the face, but I just can't see it. Any help will be greatly appreciated.

2
  • What dialect are you using? Commented Jul 29, 2014 at 15:53
  • I'm not specifying a dialect I think. It was set up by another developer... I initialize a NHibernateHelper like this whith a staticSessionContext that holds a dictionary: NHibernateHelper.Initialize(new StaticSessionContext(), new Configuration().Configure().BuildSessionFactory()); Commented Jul 29, 2014 at 16:03

1 Answer 1

7

You've got be be doing that somewhere in either code or a config somewhere. I'm using NHibernate 3.3 with the map by code and it's mapping to VARBINARY(MAX) without an issue.

Here's my property definition:

Property(x => x.Photo, map =>
{
    map.Type(NHibernateUtil.BinaryBlob);
    map.Length(Int32.MaxValue);
});

Here's my SessionFactory configuration:

Configure.DataBaseIntegration(db =>
          {
            db.Dialect<MsSql2008Dialect>();
            db.Driver<SqlClientDriver>();
            db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
            db.IsolationLevel = IsolationLevel.ReadCommitted;
            db.ConnectionStringName = System.Environment.MachineName;
            db.BatchSize = 20;
            db.Timeout = 10;
            db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
          });

Note that I'm specifying a Driver and Dialect. VARBINARY(MAX) was not supported before the MsSql2005Dialect.

Sign up to request clarification or add additional context in comments.

2 Comments

ah yes I found it in the app.config file: <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property> <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
Sorry about this guys. this mapping does work. I had another column in this table (name column) that was defined as varchar(50) but the value was 51 characters. If only the error message would mention the column perhaps I wouldn't have wasted my and your time on this.... but all is well again :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.