NHibernate 4000 Character Limit, Sigh…

Summary

NHibernate has its share of issues. One of it’s most notorious issues is the 4000 character limit. That limit reveals itself when you attempt to write to text or VARCHAR fields in your database that exceed that limit. In this post, I’m going to describe some solutions to this problem.

Mappings

One issue shows up in the mappings and I’ve mentioned this before. To get around the mapping issue for VARCHAR(MAX) fields you’ll need to add some attributes:

Map(u => u.ExtraData).CustomType("StringClob")
.CustomSqlType("varchar (MAX)").Length(Int32.MaxValue);

This sample will setup the field named “ExtraData” with a custom type of StringClob, the SQL field type of VARCHAR(MAX) and set the length to the max int value.

Stored Procedure Parameters

Another crazy place where this problem shows up is in the parameters passed to a stored procedure or a SQL call:

db.CreateSQLQuery("MyStoredProc :myparameter")
    .SetParameter("myparameter", data, NHibernate.NHibernateUtil.StringClob)
    .ExecuteUpdate();

Data is the string that exceeds 4000 characters. The NHibernate.NHibernateUtil.StringClob allows the parameter to handle a large string.

There are other methods of dealing with the mapping issue which I have not tried, here’s one interesting example on stack overflow:

string-unlimited-still-limited-to-4000-characters

Leave a Reply