仅映射几个属性,而不是使用Automapper映射所有属性 [英] Mapping only few properties instead of all using Automapper

查看:65
本文介绍了仅映射几个属性,而不是使用Automapper映射所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用automapper,到目前为止,它对我来说还算不错.我一直在将域对象映射到相应的dto,默认情况下,所有源属性都映射到其匹配的目标属性.我一直在使用如下代码:

I have recently started using automapper and it has work fine for me so far. I have been mapping domain objects to corresponding dtos and by default all source properties get mapped to their matching destination properties. I have been using code as follows:

Mapper.CreateMap<Node, NodeDto>();
var nodeDto = Mapper.Map<Node, NodeDto>( node );

现在我陷入一种情况,我只想映射源对象的某些属性.我不想将源对象中的集合属性映射到匹配的目标属性.有没有办法做到这一点?

Now I have got into a situation where I would like to map only some of the properties of the source object. There are collection properties in the source object that I do not want to be mapped to the matching destination properties. Is there a way to achieve that?

推荐答案

您可以这样指定要忽略的属性:

You could specify the properties to ignore like this:

Mapper.CreateMap<Node, NodeDto>()
      .ForMember(dest => dest.SomePropToIgnore, opt => opt.Ignore())

这篇关于仅映射几个属性,而不是使用Automapper映射所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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