Automapper映射 [英] Automapper Mapping

查看:177
本文介绍了Automapper映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图映射一个对象的属性名称,像这样:

I'm trying to map an object with property names like so:

Property_One -> PropertyOne ... etc
Sample_Property -> SampleProperty

有没有更好的方式来做到这一点,而不是单独映射每个属性到另一个?唯一的区别是下划线

Is there a better way to do this than to map individually each property to another? The only difference is the underline.

推荐答案

您需要指定下划线在源端命名约定:

You'll need to specify the underscore naming convention on the source side:

Mapper.Initialize(i => 
{
    i.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
    i.CreateMap<Source, Dest>(); 
});

您可以做的,在全球范围(如上图所示)或每档案,如果只有部分源类型遵循这个命名约定。

You can do that globally (as shown above) or per Profile, if only some of your source types follow this naming convention.

这篇关于Automapper映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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