开发者

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

开发者 https://www.devze.com 2023-02-19 09:15 出处:网络
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 Precisio开发者_JAVA百科n() and S

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 Precisio开发者_JAVA百科n() 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号