【问题标题】:Override CreateMap - Automapper覆盖 CreateMap - Automapper
【发布时间】:2021-10-13 06:56:30
【问题描述】:

我有以下地图。所有小数类型的数字都有 2 个小数位。

CreateMap<decimal, decimal>().ConvertUsing(x => decimal.Round(x, 2, MidpointRounding.AwayFromZero));

但有时,我需要更多的小数位,例如

CreateMap<T, R>.ForMember(x => x.Total, x => x.MapFrom(x => decimal.Round(x.Items.Sum(x => x.Price * x.Quantity), 3, MidpointRounding.AwayFromZero)))

是否可以覆盖某些“CreateMap”中的全局​​ CreateMap?谢谢

【问题讨论】:

标签: c# .net-core automapper


【解决方案1】:

一种选择是,当您调用 mapper.Map 时,您可以提供使用内联映射覆盖当前地图配置文件的选项,就像这样。

_mapper.Map(T, R, opt => opt.ConfigureMap().ForMember(x => x.Total, x => x.MapFrom(x => decimal.Round(x.Items.Sum(x => x.Price * x.Quantity), 3, MidpointRounding.AwayFromZero)));

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 1970-01-01
    相关资源
    最近更新 更多