Geeks With Blogs
Łukasz Kuryło's blog

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 (link)

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)")));

Posted on Monday, July 11, 2011 6:50 PM NHibernate | Back to top


Comments on this post: [NHibernate]Mapping a string field as nvarchar(MAX) in SQL Server using mapping by code

Comments are closed.
Comments have been closed on this topic.
Copyright © Łukasz Kuryło | Powered by: GeeksWithBlogs.net