Search code examples
fluent-nhibernatenullableautomappingconvention

Fluent Nhibernate Automap convention for not-null field


Could some one help, how would I instruct automap to have not-null for a column?

public class Paper : Entity
{
    public Paper() { }

            [DomainSignature]
            [NotNull, NotEmpty]
            public virtual string ReferenceNumber { get; set; }

            [NotNull]
            public virtual Int32 SessionWeek { get; set; }
}

But I am getting the following:

 <column name="SessionWeek"/>

I know it can be done using fluent-map. but i would like to know it in auto-mapping way.


Solution

  • Thank you. Also, for reference properties ReferenceConvention need to be done. This is the code that works:

    public class ColumnNullConvention : IPropertyConvention
    {
        public void Apply(IPropertyInstance instance)
        {
            if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false))
                instance.Not.Nullable();
        }
    
    }  public class ReferenceConvention : IReferenceConvention
    {
        public void Apply(FluentNHibernate.Conventions.Instances.IManyToOneInstance instance)
        {
            instance.Column(instance.Property.Name + "Fk");
    
    
            if (instance.Property.MemberInfo.IsDefined(typeof(NotNullAttribute), false))
                instance.Not.Nullable();
    
        }
    }