18

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)

Community
  • 1
  • 1
Nicolas Cadilhac
  • 4,680
  • 5
  • 41
  • 62

2 Answers2

21

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.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
DanP
  • 6,310
  • 4
  • 40
  • 68
  • In an hbm mapping file, simply use type="StringClob" instead of type="string" and you're done. This definitely works for NH 3.3, but try it with for any NH version >= 3.0. – Oliver Jun 13 '12 at 15:58
  • Note that StringClob will generate a nvchar(255) on SQL Server 2008, and instead the workaround is to use a .Length(10000). This will create a nvchar(max) column type. – Astaar Mar 29 '13 at 14:20
17

This mapping should work:

<property name="TheProperty" type="StringClob">
  <column name="TheColumn" sql-type="nvarchar(max)" />
</property>

Just look for the fluent equivalent.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • 2
    do you think this will ever be treated properly in NHib 3? Kind of sad to see the sql server specific details leak through in the mapping like this... – DanP Sep 14 '10 at 00:15
  • I believe there already is one: http://216.121.112.228/browse/NH-2302 Incidentally...would you be interested in a donated domain name for the JIRA or something? Having an IP based url seems a little...inconvenient ;) – DanP Sep 16 '10 at 18:47
  • @DanP, the domain name is jira.nhforge.org, I have no clue why it's being using the IP for some months now. I don't administer it, but you can ask in the dev list. – Diego Mijelshon Sep 16 '10 at 21:12
  • 2
    The fluent equivalent is `.CustomType("StringClob").CustomSqlType("nvarchar(max)")` – David Beckman Feb 23 '12 at 22:18
  • what if you wanted to go against something other than sql server? No way to set the length of the field to -1 or something and let the dialect engine sort it out? – Jeremy Holovacs Mar 08 '12 at 02:38
  • Nope, only fixed in not yet released v4.1. See [NH-3252](https://nhibernate.jira.com/browse/NH-3252). (Founded at the end of [this answer](/a/12106405/1178314) to the same issue with hbm mapping. – Frédéric Mar 16 '16 at 20:33