简体   繁体   English

忽略 AutoMapper 上的嵌套属性

[英]Ignoring nested property on AutoMapper

My ViewModel has a sequence of related entities like that:我的 ViewModel 有一系列相关实体,如下所示:

Store.Package.Item商店.包裹.项目

I am trying to ignore the last Item element on my path, when mapping from the viewmodel to the entity.从视图模型映射到实体时,我试图忽略路径上的最后一个 Item 元素。 That's my mapping:这是我的映射:

  CreateMap<Store, StoreViewModel>().ReverseMap().ForPath(s => s.Package.Item, opt => opt.Ignore());

The problem is that the entire Package element is being ignored, but I need to ignore just the Item property.问题是整个 Package 元素都被忽略了,但我只需要忽略 Item 属性。

Could anyone help me on this?有人可以帮我吗?

Regards问候

You can add another mapping from the Package type into itself and ignore the item property您可以从 Package 类型添加另一个映射到自身并忽略 item 属性

CreateMap<Store, StoreViewModel>().ReverseMap();
CreateMap<*PackageClass*, *PackageClass*>().ReverseMap()
.ForMember(s => s.Item, opt => opt.Ignore());

PackageClass is the type of the Store.Package property PackageClass是Store.Package属性的类型

You could do something like this:¨你可以这样做:¨

Mapper.Initialize(c=>   
    {
        c.CreateMap<Store, StoreViewModel>().ReverseMap();//.ForPath(s => s.Package.Item, opt => opt.Ignore()));
        c.CreateMap<Package, PackageViewModel>().ReverseMap().ForPath(s=> s.Item,opt=> opt.Ignore());       
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM