Search code examples
.netnhibernatefluent-nhibernate

How to store a non truncated varchar(max) string with NHibernate and Fluent NHibernate


My DB schema has a string as a varchar(max). I have read the other questions concerning setting Length to more than 4000 or 8000 so that it really generates a (n)varchar(max) in the mapping but when I use Length(10000) in my mapping class, the hbm file actually shows length="10000" and if I save an entity with more than 10000 chars, it is actually truncated to exactly 10000 chars.

I don't want any truncation.

(using NH3-alpha2 and FNH trunk)


Solution

  • It would appear that this is an old problem which is now resurfacing in NHibernate 3.x builds; you can read about the workarounds here.

    <property name="Blob" column="Blob" type="StringClob" />
    <!-- this generates an nvarchar(max), data is truncated at the length specified: string(10000) -->
    <property name="BlobLength" column="BlobLength" type="StringClob" length="10000" />
    <!-- this mapping works! for generation, reading and writing -->
    <property name="BlobSqlType" type="StringClob" >
        <column name="BlobSqlType" sql-type="nvarchar(max)" />
    </property>
    

    Calling CustomType("StringClob") should fix the problem.

    Note: I have updated the original link I posted as it was outdated.