Archive, NHibernate
     

NHibernate and Text fields

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:

[NHMA.Property(Name="SomeData")]
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; }
}

After telling NHibernate that the field was of type StringClob it no longer truncates large strings, and correctly treated it as a Text field.

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.

 

About Jason

Jason is an experienced entrepreneur & software developer skilled in leadership, mobile development, data synchronization, and SaaS architecture. He earned his Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *