Reputation: 4713
I have 2 fields in DomainModel (CreatedOn,ModifiedOn) which are not in my ViewModel. How can I put ignore on Source fields when mapping from DomainModel to ViewModel. Please fix below code.
Mapper.CreateMap<DomainModel, ViewModel>()
.ForMember(d => d.CreatedOn, opt => opt.Ignore())
.ForMember(d => d.ModifiedOn, opt => opt.Ignore());
Upvotes: 0
Views: 330
Reputation: 1038830
You don't need to specify anything about those fields. Just:
Mapper.CreateMap<DomainModel, ViewModel>();
If the CreatedOn
and ModifiedOn
properties don't exist on your view model when mapping between DM and VM they will be simply ignored.
Upvotes: 1
Reputation: 10792
I believe that AutoMapper will only try to populate those fields in the target. As long as the target doesn't have fields that happen to be in the source, those fields won't exist in the target after mapping is complete.
However, if the fields did happen to exist in the target, it looks like your syntax is correct (although for consistency with all examples I have seen - you might use dest => dest.CreatedOn instead of d => d.CreatedOn - but I don't think that will break it).
Upvotes: 0