Saving large string to Sql Server varchar(max) column truncates to 4000

276 views
Skip to first unread message

flynhigh

unread,
Mar 6, 2013, 11:50:05 AM3/6/13
to sharp...@googlegroups.com
Hi All,

I'm experiencing unexpected behavior of the NHibernate, I believe.
The column type is varchar(max) and I'm trying to save usually small but sometimes large, less than 100KB text file.
I was thinking varchar(max) should be able to store up to 2GB.
The other day when I tried to troubleshoot the production error I found that the column has 4000 character truncated text which means I couldn't recover from the error!

Quick Googling revealed that there're two issue/resolution involved: one being specifying the type in string field or size (http://www.primordialcode.com/blog/post/nhibernate-prepare_sql-considerations-mapping-long-string-fields), the other being sql server parameter is truncating (https://nhibernate.jira.com/browse/NH-2528).

Has anyone experienced this issue and figured how to solve in S#arp Lite?

Thanks in advance!

Terry

Paul Syfrett

unread,
Sep 6, 2013, 5:55:09 PM9/6/13
to sharp...@googlegroups.com
I've found that you can do this in the overrides by setting the length of the field to something over 4000 (e.g. 4001).  Like so:
    public class MyEntityOverride : IOverride
    {
        public void Override(ModelMapper mapper)
        {
            mapper.Class<MyEntity>(map =>
            {
                // this is required when using a varchar(max).  Any value over 4000 will work.
                map.Property(x => x.MyBigColumn, p => p.Length(4001));
            });
        }
    }

Paul
Reply all
Reply to author
Forward
0 new messages