Fluent Nhibernate Automap convention for not-null field

北城以北 提交于 2019-12-01 04:01:36

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();

    }
}

Here is the way I do it, basically taken from the link you see in the code. There are some other useful conventions there as well

HTH,
Berryl

/// <summary>
/// If nullability for the column has not been specified explicitly to allow NULL, then set to “NOT NULL”.
/// </summary>
/// <remarks>see http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/</remarks>
public class ColumnNullabilityConvention : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
        criteria.Expect(x => x.Nullable, Is.Not.Set);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Not.Nullable();
    }
}

If you are mostly happy with Automapping results but occasionally need to override it for say a couple of properties in a class I find implementing a IAutoMappingOverride for that class the easiest way to achieve that:

public class UserMappingOverride : IAutoMappingOverride<User>
{
      public void Override(AutoMapping<User> mapping)
      {
          mapping.Map(x => x.UserName).Column("User").Length(100).Not.Nullable();
      }
}

And then use them like this:

AutoMap.AssemblyOf<User>().UseOverridesFromAssemblyOf<UserMappingOverride>();

Similar to ClassMaps - but you don't need to describe every field in the class. This approach is very similar to the Entity Framework's Code First Fluent API way.

public class Paper Map : IAutoMappingOverride<Paper >
{
    public void Override(AutoMapping<Paper> mapping)
    {
        mapping.Map(x => x.ReferenceNumber).Not.Nullable();
    }
}

Int32 is not nullable type by default. Int32? is nullable, so you make it non-nullable just by specifying it as Int32.

You can use conventions to do this automatically. I am not sure which convention to use, but have a look at FluentNHibernate.Conventions.Instances to find the right one. It'll look like this.

public class ColumnConvention : IColumnConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.ColumnInstance instance)
    {
        if (instance.EntityType.IsDefined(typeof(NotNullAttribute), false))
            instance.NotNull = true;
    }

    public void Apply(FluentNHibernate.Conventions.Instances.IColumnInstance instance)
    {
        return;
    }
}

Just add this convention to your automapping.

I find more often than not, my columns are not null, so I prefer make this convention and only specify columns as nullable:

  /// <summary>
  /// Indicates that a column should allow nulls 
  /// </summary>
  [Serializable]
  [AttributeUsage(AttributeTargets.Property)]
  public class NullableAttribute : Attribute
  {
  }



 public class ColumnIsNotNullByDefaultConvention : IPropertyConvention, IPropertyConventionAcceptance
  {
    public void Apply(IPropertyInstance instance)
    {
      instance.Not.Nullable();
    }

    public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
    {
      criteria.Expect(c => !c.Property.MemberInfo.IsDefined(typeof(NullableAttribute), false));
    }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!