我有一种情况,我想忽略基类中定义的类的某些属性.
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忽略子类映射中的基类属性的问题
发布评论