views:

536

answers:

2

When ever you set a string value in fluent NHibernate it alwasy sets the DB vales to Nvarchar(255), I need to store quite a lot of long string which are based on user inputs and 255 is impractical.

Just to add this is an issue with the automapper as I am using fluent NHibernate to build the database.

+2  A: 

Setting the length to anything over 4001 will generate an NVarchar(MAX)...

.WithLengthOf(10000);

See here for more detail...

http://serialseb.blogspot.com/2009/01/fluent-nhibernate-and-nvarcharmax.html

Russell Giddings
This looks like it would work for a single string value, But i need to do this to close to 300 data fields so am looking for an easy way to just override every nvarchar(255) and set it as an nvarchar(Max)
TheAlbear
+3  A: 

Adding this convention will set the default length for string properties to 10000. As others have noted, this will be a nvarchar(max) column.

public class StringColumnLengthConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Type == typeof(string)).Expect(x => x.Length == 0);
    }
    public void Apply(IPropertyInstance instance)
    {
        instance.Length(10000);
    }
}

Conventions can be added to an automap configuration like this:

Fluently.Configure()
    .Mappings( m =>
        m.AutoMappings.Add( AutoMap.AssemblyOf<Foo>()
        .Conventions.Add<StringColumnLengthConvention >()))

For more information, see Conventions in the Fluent NHibernate wiki.

Lachlan Roche
Just what I was looking for, oh and for anyone else you Add this line to the GetConventions() method in utoPersistenceModelGenerator filec.Add<StringColumnLengthConvention>();
TheAlbear