How can I Change the default scale and precision of decimals in Fluent NHibernate?

亡梦爱人 提交于 2019-12-22 04:34:29

问题


In the application I'm building, I have many decimal fields with a specific precision and scale that need to be mapped from a database. I can achieve this by using the Precision() and Scale() methods:

public class ClassAMap : ClassMap<ClassA>
{
    public ClassAMap ()
    {
        Map(x => x.Value).Precision(22).Scale(12);
    }
}

Is there any way to change the default precision and scale for decimals, so I don't need to remember to add the calls to Precision() and Scale() for every decimal mapped?


回答1:


You can define a PropertyConvention. Following is the general idea. (NOT tested)

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

Make sure you include this convention when Fluent is configured.



来源:https://stackoverflow.com/questions/5426496/how-can-i-change-the-default-scale-and-precision-of-decimals-in-fluent-nhibernat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!