如何在Fluent NHibernate中更改小数的默认比例和精度?
在我正在构建的应用程序中,我有许多十进制字段,需要从数据库映射特定的精度和比例。 我可以通过使用Precision()
和Scale()
方法来实现这一点:
public class ClassAMap : ClassMap { public ClassAMap () { Map(x => x.Value).Precision(22).Scale(12); } }
有没有办法更改小数的默认精度和小数位数,所以我不需要记得为每个小数映射添加对Precision()
和Scale()
的调用?
您可以定义PropertyConvention。 以下是一般的想法。 (未测试)
public class DecimalConvention : IPropertyConvention { public void Apply(IPropertyInstance instance) { if (instance.Type == typeof(decimal) || instance.Name == "Value") //Set the condition based on your needs { instance.Precision(22).Scale(12); } } }
确保在配置Fluent时包含此约定。