Search code examples
fluentautomappingfluent-nhibernate-mapping

Override FluentNHibernate Auto mapping


I need change for the next class data type assumed by default FluentNHibernate Automapping

public class plaparte
{
public virtual int id { get; private set; }
public virtual int vivos { get; set; }
public virtual int lesionados { get; set; }
public virtual int quemados { get; set; }
public virtual int muertos { get; set; }
public virtual int otros { get; set; }
public virtual string colaboracion { get; set; }
public virtual decimal hectareas { get; set; }
public virtual string reconocimiento { get; set; }
public virtual string disposiciones { get; set; }
public virtual plaserv plaserv { get; set; }
}
}

I need for this class only the string type to be converted into TEXT in database If I change by

public virtual string[] reconocimiento { get; set; }

FluentNHibernate takes a BLOB data type

I can do something like?

public class plaparteMappingOverride : IAutoMappingOverride<plaparte>
{
public void Override(AutoMapping<plaparte> mapping)
{
  Map(x => x.disposiciones).CustomSqlTypeIs("TEXT");
}
}

Solution

  • To solve the problem I am using:

    using System.ComponentModel.DataAnnotations;
    ...
    public class plaparte
    {
    ...
    [StringLength(4000)]
    public virtual string disposiciones { get; set; }
    

    To create TEXT fields

    [Update]

    For work I need to create the next class

    class StringLengthConvention : AttributePropertyConvention<StringLengthAttribute>
    {
        protected override void Apply(StringLengthAttribute attribute, IPropertyInstance instance)
        {
            instance.Length(attribute.MaximumLength);
        }
    }
    

    is also necessary to add the convention Fluent automap Like

        static AutoPersistenceModel CreateAutomappings()
        {
            return AutoMap.AssemblyOf<plaparte>(new mapAutomapConfiguration())
                .Conventions.Setup(c =>
                {
                    c.Add<StringLengthConvention>();
                });
        }