Rinaldo Ferreira
Rinaldo Ferreira

Reputation: 21

Ignoring nested property on AutoMapper

My ViewModel has a sequence of related entities like that:

Store.Package.Item

I am trying to ignore the last Item element on my path, when mapping from the viewmodel to the entity. 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.

Could anyone help me on this?

Regards

Upvotes: 2

Views: 1291

Answers (2)

Ben Adam
Ben Adam

Reputation: 11

You can add another mapping from the Package type into itself and ignore the item property

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

PackageClass is the type of the Store.Package property

Upvotes: 1

Brian Prang Thumann
Brian Prang Thumann

Reputation: 1

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());       
    });

Upvotes: 0

Related Questions