Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
13 lines (11 sloc) 4.79 KB
---
layout: post
title: Fluent-nhibernate and nvarchar(max)
date: '2009-01-06T14:41:00.001Z'
tags: [nhibernate]
modified_time: '2009-01-06T14:41:47.620Z'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-655023133802570413
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2009/01/fluent-nhibernate-and-nvarcharmax.html
---
<p>As you may know, text and ntext types are being depreciated in sql server 2005+, and replaced by varchar(max) and nvarchar(max).</p> <p>If like me you rely on nhibernate to generate your database, the trick for getting nvarchar(max) is to set the length of the field to 10000. I wrote a small extension method to do this.</p> <div style="padding-right: 10px; padding-left: 10px; font-size: 10pt; background: #252525; padding-bottom: 10px; color: #e0e0e0; padding-top: 10px; font-family: consolas, courier new"> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: #00bfff">public</span> <span style="color: #00bfff">static</span> <span style="color: #00bfff">class</span> <span style="color: #00d2d2">PropertyMapExtensions</span></p> <p style="margin: 0px">&#160;&#160;&#160; {</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">public</span> <span style="color: #00bfff">static</span> <span style="color: #00d2d2">PropertyMap</span> WithMaxSize(<span style="color: #00bfff">this</span> <span style="color: #00d2d2">PropertyMap</span> map)</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00bfff">return</span> map<span style="color: #a6a0e0">.</span>WithLengthOf(<span style="color: #80ff00">10000</span>);</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</p> <p style="margin: 0px">&#160;&#160;&#160; }</p> </div> <p>I use the automapping functionality, so this is what it then looks like.</p> <div style="padding-right: 10px; padding-left: 10px; font-size: 10pt; background: #252525; padding-bottom: 10px; color: #e0e0e0; padding-top: 10px; font-family: consolas, courier new"> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #00d2d2">AutoPersistenceModel</span><span style="color: #a6a0e0">.</span>MapEntitiesFromAssemblyOf<span style="color: #a6a0e0">&lt;</span><span style="color: #00d2d2">Movie</span><span style="color: #a6a0e0">&gt;</span>()</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a6a0e0">.</span>Where(t <span style="color: #a6a0e0">=&gt;</span> t<span style="color: #a6a0e0">.</span>Namespace <span style="color: #a6a0e0">==</span> (<span style="color: #00bfff">typeof</span>(<span style="color: #00d2d2">Entity</span>)<span style="color: #a6a0e0">.</span>Namespace) <span style="color: #a6a0e0">&amp;&amp;</span> <span style="color: #a6a0e0">!</span>t<span style="color: #a6a0e0">.</span>IsAbstract <span style="color: #a6a0e0">&amp;&amp;</span> t<span style="color: #a6a0e0">.</span>IsPublic)</p> <p style="margin: 0px">&#160;</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a6a0e0">.</span>ForTypesThatDeriveFrom<span style="color: #a6a0e0">&lt;</span><span style="color: #00d2d2">Movie</span><span style="color: #a6a0e0">&gt;</span>(x <span style="color: #a6a0e0">=&gt;</span></p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x<span style="color: #a6a0e0">.</span>Map(m <span style="color: #a6a0e0">=&gt;</span> m<span style="color: #a6a0e0">.</span>LongSynopsis)<span style="color: #a6a0e0">.</span>WithMaxSize();</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; x<span style="color: #a6a0e0">.</span>Map(m <span style="color: #a6a0e0">=&gt;</span> m<span style="color: #a6a0e0">.</span>Notes)<span style="color: #a6a0e0">.</span>WithMaxSize();</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; })</p> <p style="margin: 0px">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style="color: #a6a0e0">.</span>Configure(configuration);</p> </div> <p></p> And if you’re interested in all things fluent-nhibernate, why not come and listen to <a href="http://skillsmatter.com/event/open-source-dot-net/open-source-dot-net-exchange">my talk about it on the 22nd of January at SkillsMatter</a>?