Override for fluent NHibernate for long text strings nvarchar(MAX) not nvarchar(255)

前端 未结 6 1661
时光说笑
时光说笑 2020-12-09 07:26

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

6条回答
  •  心在旅途
    2020-12-09 08:20

    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 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()
            .Conventions.Add()))
    

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

提交回复
热议问题