我怎样才能更改功能NHibernate小数的默认比例和precision?小数、比例、功能、precision

2023-09-03 16:11:15 作者:丶青春微凉不离伤

在应用我建设,我有一个特定的precision和规模需要从数据库映射多个小数领域。我可以使用 precision()和实现这个量表()方法:

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

有没有办法来改变默认的precision和规模为小数,所以我并不需要记住的调用添加到 precision()量表()为每一位小数映​​射?

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?

推荐答案

您可以定义PropertyConvention。以下是一般的想法。 (未测试)

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.

 
精彩推荐
图片推荐