40

I am trying to insert some very long text into a string prop - it worked perfectly fine with LinqToSql, now I have switched over to NHibernate and want to save the same entity, but nHibernate throws the above exception.

How can I fix this?

Originally my props were defined as:

        Map(x => x.Content, "fT_Content").Nullable();
        Map(x => x.Fields, "fT_Fields").Nullable();

now they are: this works but why do I have to do this?

        Map(x => x.Content, "fT_Content").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();
        Map(x => x.Fields, "fT_Fields").CustomSqlType("nvarchar(max)").Length(Int32.MaxValue).Nullable();

Note: I have the latest nhibernate using nuget.

For ref here are the fields:

    public virtual string Content
    {
        get;
        set;
    }

    public virtual string Fields
    {
        get;
        set;
    }

I want to avoid going to live production and all of a sudden inserts stop working on this table....

5 Answers 5

75

This is a well known issue with NHibernate handling nvarchar(max), see :

http://geekswithblogs.net/lszk/archive/2011/07/11/nhibernatemapping-a-string-field-as-nvarcharmax-in-sql-server-using.aspx

For some years now, I have been file mapping nvarchar(max) columns to StringClob without encountering any problem :

<property name="myProp" column="MY_PROP" not-null="true" type="StringClob" access="property"></property>   

This link (from the comments) describes the fluent mapping required to fix this issue:

https://www.tritac.com/nl/blog/fluent-nhibernate-nvarchar-max-fields-truncated-to-4000-characters/

Map(x => x.Description).CustomType("StringClob").CustomSqlType("nvarchar(max)");
5
  • so based on the link - I have correctly mapped using fluent nhibernate?
    – Haroon
    Oct 4, 2012 at 8:18
  • Based on the link, yes. But I never tested it, as StringClob works perfectly for me. I have a legacy DB with ntext columns. Using StringClob allows me to keep the same mapping when converting a column from ntext to nvarchar(max)
    – jbl
    Oct 4, 2012 at 9:10
  • what is the fluent mapping equivalent for stringclob? i.e. what is the sytanx?
    – Haroon
    Oct 4, 2012 at 9:15
  • 1
    I don't know. This link should be helpfull : tritac.com/…
    – jbl
    Oct 4, 2012 at 9:31
  • A quick work-around that we used was to add .Length(10000) to the mapping file for the article, since our article was added through a WYSIWYG editor. :)
    – Jamie
    Feb 28, 2013 at 20:38
9

Default maximum string length in NHibernate is 4000 characters.

8

If you use nHibernate Mapping By Code (Not Fluent) I found here the solution:

https://groups.google.com/forum/#!topic/nhusers/uDAcP4BqFUU

Property(x => x.Description, c =>
   {
      c.Column("Product");
      c.Type(NHibernateUtil.StringClob);
   }
);
3

It's quite an old question and it seems there is a more straightforward solution now. I had the same issue and setting the length of the column to Int32.MaxValue in the mapping solved the problem :

Map(_ => _.Body).Length(Int32.MaxValue);
1
  • 1
    In 2019, the simplest and cleanest answer. Works at least with NH4.0. In *.hbm.xml format: <property name="Text" type="string" length="1000000" />. Of course, use whatever number suits you for the length attribute.
    – Oliver
    Apr 5, 2019 at 9:17
0

I hit a similar problem but using a CustomType (derived from IUserType). It turns out to be easier to fix than without a custom type. All you need to do is define SqlTypes to explicitly return the longer string length type you want.

public SqlType[] SqlTypes
{
    get
    {
        // Explicitly specify the string max length 
        return new SqlType[] { SqlTypeFactory.GetString(Int32.MaxValue / 2) };
    }
}

This solved the problem for us quite nicely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.