Tigraine

Daniel Hoelbling-Inzko talks about programming

Mapping large text or binary values with NHibernate

Posted by Daniel Hölbling on December 19, 2008

I encountered the following error when trying to map a large String to my SQL2005 database:

SQL Error Code -2146232060: “String or binary data would be truncated”

The issue here is that Nhibernate maps all string values by default as nvarchar(255) and so inserting something bigger to a field causes this nasty sql error. My mapping declaration looked like this:

<property name="Comments" />

After some searching I found Ayende’s post on NHibernate and large text fields gotchas that almost solved the issue, except for one thing, I didn’t know where to put the sql-type attribute. Turns out it’s defined in chapter 15 of the NHibernate doc (while mapping files are chapter 5).  
The sql-type=”NTEXT” attribute can only reside on the <column node beneath the <property node. So the correct mapping looks like this:

<property name="Comments" type="StringClob">
  <column name="Comments" sql-type="NTEXT"/>
</property>

If you don’t define the sql-type attribute even the StringClob field will be created as a nvarchar(255) by NHibernate (but it can map to a NText field if the schema exists).

Filed under net, nhibernate
comments powered by Disqus

My Photography business

Projects

dynamic css for .NET

Archives

more