Tuesday, 6 January 2009

Fluent-nhibernate and nvarchar(max)

As you may know, text and ntext types are being depreciated in sql server 2005+, and replaced by varchar(max) and nvarchar(max).

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.

    public static class PropertyMapExtensions

    {

        public static PropertyMap WithMaxSize(this PropertyMap map)

        {

            return map.WithLengthOf(10000);

        }

    }

I use the automapping functionality, so this is what it then looks like.

            AutoPersistenceModel.MapEntitiesFromAssemblyOf<Movie>()

                .Where(t => t.Namespace == (typeof(Entity).Namespace) && !t.IsAbstract && t.IsPublic)

 

                .ForTypesThatDeriveFrom<Movie>(x =>

                {

                    x.Map(m => m.LongSynopsis).WithMaxSize();

                    x.Map(m => m.Notes).WithMaxSize();

                })

                .Configure(configuration);

And if you’re interested in all things fluent-nhibernate, why not come and listen to my talk about it on the 22nd of January at SkillsMatter?

1 comment:

Rodj said...

FYI length of 4001 is enough to get nvarchar(max), however any number above this will work.

Post a Comment