nhibernate varbinary(max) byte array mapping
-
21-12-2019 - |
Question
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.
Solution
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.