使用Automapper忽略子类映射中的基类属性的问题 [英] Issue with ignoring base class property in child classes mappings using Automapper

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

问题描述

我有一种情况,我想忽略基类中定义的类的某些属性.

I have a scenario where I would like to ignore some properties of classes defined in base class.

我有一个像这样的初始映射

I have an initial mapping like this

   Mapper.CreateMap<Node, NodeDto>()
                .Include<Place, PlaceDto>()
                .Include<Asset, AssetDto>();

然后我更像这样自定义它,以忽略基类NodeDto中定义的属性之一

Then I customised it more like this to ignore one of the properties defined in base class NodeDto

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

但是,当我尝试映射,放置到PlaceDto或资产到AssetDto时,ChildNodes属性不会被忽略.所以我最终做了这样的安慰

However when I try to map, Place to PlaceDto or Asset to AssetDto, the ChildNodes property does not get ignored. So I ended up doing soething like this

  Mapper.CreateMap<Node, NodeDto>()
                .ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
            Mapper.CreateMap<Place, PlaceDto>()
                .ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
            Mapper.CreateMap<Asset, AssetDto>()
                .ForMember(dest => dest.ChildNodes, opt => opt.Ignore());

由于我有许多NodeDto的子类,因此上述过程很麻烦,我想知道是否有更好的方法吗?

Since I have lots of child classes for NodeDto, the above process is cumbersome, and I would like to know if there is a better approach?

谢谢 纳比尔(Nabeel)

Thanks Nabeel

推荐答案

对不起,但是,没有别的办法了,这就是automapper的工作方式

sorry but, no, there isn't any other way, that's how automapper works

这篇关于使用Automapper忽略子类映射中的基类属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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