Automapper:忽略的条件 [英] Automapper: Ignore on condition of
本文介绍了Automapper:忽略的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能忽略映射取决于源属性的值一员?
Is it possible to ignore mapping a member depending on the value of a source property?
例如,如果我们有:
public class Car
{
public int Id { get; set; }
public string Code { get; set; }
}
public class CarViewModel
{
public int Id { get; set; }
public string Code { get; set; }
}
我正在寻找类似
Mapper.CreateMap<CarViewModel, Car>()
.ForMember(dest => dest.Code, opt => opt.Ignore().If(source => source.Id == 0))
到目前为止,我有过使用两个不同的视图模型,并为每一个创建不同的映射的唯一解决方案。
So far the only solution I have is too use two different view models and create different mappings for each one.
推荐答案
忽略()的特点是严格的会员你永远映射,因为这些成员也在配置验证跳过。我查了几个选项,但它看起来并不像之类的自定义值解析器会做的伎俩。相反,我将着眼于增加一个条件跳过配置选项,如:
The Ignore() feature is strictly for members you never map, as these members are also skipped in configuration validation. I checked a couple of options, but it doesn't look like things like a custom value resolver will do the trick. Instead, I'll look at adding a conditional Skip configuration option, like:
Mapper.CreateMap<CarViewModel, Car>()
.ForMember(dest => dest.Code, opt => opt.Condition(source => source.Id == 0))
这篇关于Automapper:忽略的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文