41

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....

Haroon
  • 3,402
  • 6
  • 43
  • 74

5 Answers5

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)");
Chris R. Donnelly
  • 3,086
  • 3
  • 28
  • 28
jbl
  • 15,179
  • 3
  • 34
  • 101
  • so based on the link - I have correctly mapped using fluent nhibernate? – Haroon Oct 04 '12 at 08: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 04 '12 at 09:10
  • what is the fluent mapping equivalent for stringclob? i.e. what is the sytanx? – Haroon Oct 04 '12 at 09:15
  • 1
    I don't know. This link should be helpfull : http://www.tritac.com/bp-21-fluent-nhibernate-nvarcharmax-fields-truncated-to-4000-characters – jbl Oct 04 '12 at 09: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 '13 at 20:38
9

Default maximum string length in NHibernate is 4000 characters.

beauXjames
  • 8,222
  • 3
  • 49
  • 66
Mr Mush
  • 1,538
  • 3
  • 25
  • 38
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);
bN_
  • 772
  • 14
  • 20
  • 1
    In *2019*, the simplest and cleanest answer. Works at least with NH4.0. In `*.hbm.xml` format: ``. Of course, use whatever number suits you for the `length` attribute. – Oliver Apr 05 '19 at 09: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.

Tevya
  • 836
  • 1
  • 10
  • 23