Automapper的用法时,属性名称不同 [英] Usage of Automapper when property names are different

查看:829
本文介绍了Automapper的用法时,属性名称不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用AutoMapper从Codeplex上为我的目标对象有田结尾的所有属性,即车市场和源对象只是城市。

We are using AutoMapper from Codeplex and for me the destination object has all the properties ending with 'Field', ie cityField and the source object has just city.

我可以使用下面的代码来实现,但所有的属性都只是'现场'后缀,并有20个属性。

I can use the below code to achieve but all of the properties are just suffixed with 'Field' and there are 20 properties.

.ForMember(dest => dest.cityField, opt => opt.MapFrom(origin => origin.City));



有没有其他办法忽略田字时,绘图等,它可以映射,而不使用?.ForMember()20次。

Is there any other way to ignore 'Field' word when mapping and so that it can map without using .ForMember() 20 times?

推荐答案

您可以尝试识别后缀:

Mapper.Initialize(cfg => {
    cfg.RecognizePostfixes("Field");
    cfg.CreateMap<Source, Dest>();
});



认识前缀也适用本地配置文件,如果它只是一组地图,这适用于

Recognizing prefixes also works local to profiles, if it's just a set of maps that this applies to.

这篇关于Automapper的用法时,属性名称不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆