问题描述
据我了解,在NHibernate的,使用映射通过代码,我可以指定像这样一个十进制属性的精确度和规模:
I understand that in NHibernate, using mapping by code, I can specify the precision and scale of a decimal property like so:
Property(
x => x.Dollars,
m =>
{
m.Precision(9);
m.Scale(6);
}
);
这是不错,但我不知道是否有办法,我可以很容易地映射的 ALL 在一个简单的方法在所有类别中的小数属性。这似乎那种疯狂,我将不得不通过我所有的映射,并通过手工更新它们。有谁知道如何可以做到这一点?
That is nice, but I was wondering if there was a way that I could easily map ALL of the decimal properties in all of the classes in an easy way. It seems kind of crazy that I would have to go through all of my mappings and update each of them by hand. Does anyone know how this can be achieved ?
推荐答案
使用 BeforeMapProperty
在ModelMapper: -
Use the BeforeMapProperty
on the ModelMapper:-
var mapper = new ModelMapper();
mapper.BeforeMapProperty += (inspector, member, customizer) => {
if (member.LocalMember.GetPropertyOrFieldType() == typeof (decimal))
{
customizer.Precision(9);
customizer.Scale(6);
}
};
唯一的其他东西补充的是删除所有出现: -
The only other thing to add is remove all occurrences of:-
m => { m.Precision(9); m.Scale(6); }
这是因为这些将覆盖您约定设置映射类 BeforeMapProperty
除非你有不同的尺度或其他精度小数。
from your mapping classes as these will override your convention set in BeforeMapProperty
unless you have other decimals that have different scales or precisions.
这篇关于NHibernate的:具有相同的精度和规模都小数地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!