Archive, NHibernate

NHibernate and Text fields

Jason / October 11, 2006

In my last post about NHibernate I mentioned using NHibernate.Mapping.Attributes to simplify things.
Since my last post I have had the need to store rather large text data in a SQL Server Text field. My initial attempt was to simply create a C# string property that represented the field as such:

public string SomeData {
    get { return _someData; }
    set { _someData = value; }

I was shocked to learn that the above did not work correctly. What I was experiencing was NHibernate was silently truncating my data to 3000 characters (or maybe it was 4000, I can’t remember exactly). The reason for this appears to be that NHibernate assumes the field type to be of varchar not Text.

The solution to this problem is to inform NHibernate of the field’s true type. As it turns out this can be done by specifying it as type ‘StringClob’.

[NHMA.Property(Name="SomeData",Type="StringClob")] public string SomeData { get { return _someData; } set { _someData = value; } } read more