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

前端 未结 5 1732
你的背包
你的背包 2020-12-28 12:28

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 enti

相关标签:
5条回答
  • 2020-12-28 12:51

    Default maximum string length in NHibernate is 4000 characters.

    0 讨论(0)
  • 2020-12-28 12:53

    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)");
    
    0 讨论(0)
  • 2020-12-28 12:54

    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);
       }
    );
    
    0 讨论(0)
  • 2020-12-28 12:55

    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);
    
    0 讨论(0)
  • 2020-12-28 13:06

    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.

    0 讨论(0)
提交回复
热议问题