使用Automapper忽略子类映射中的基类属性的问题

编程入门 行业动态 更新时间:2024-10-28 14:30:35
本文介绍了使用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忽略子类映射中的基类属性的问题

本文发布于:2023-11-09 10:49:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:子类   类属   Automapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!