[NHibernate]Mapping a string field as nvarchar(MAX) in SQL Server using mapping by code

I'm diving in the NHibernate 3.2 mapping by code recently.

I throught that, mapping a string with specific max length to the appropriate database table will be easy, but it wasn't. I lost some time to figure it out.

At the beginning I mapped a string field in that way

Property<string>(x => x.Title, x => x.Length(Int32.MaxValue)); After generating the tables in database with my schema, unfortunatelly this field was generated as a nvarchar(255) and I didn't know why.

We can deal with it in few ways. 1. We can manually set the length to the value greater than 4001

Property<string>(x => x.Title, x => x.Length(4002)); 2. We can set the type in this way:

Property(x => x.Title, x => x.Type(NHibernateUtil.StringClob))

3. Or we can set manually the sql type:

Property<string>(x => x.Title, x => x.Column(z => z.SqlType("nvarchar(max)")));

This article is part of the GWB Archives. Original Author: Łukasz Kuryło

New on Geeks with Blogs