Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

nhibernate - The length of the string value exceeds the length configured in the mapping/parameter

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

question from:https://stackoverflow.com/questions/12708171/the-length-of-the-string-value-exceeds-the-length-configured-in-the-mapping-para

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...