Here is how it looks like right now. DestinationA and DestinationB are derived from some DestinationBase class. And I need to ignore some common properties for all these derived class. Is there anyway to apply these ignore options globally without having to repeat for all derived destination classes?
Mapper.CreateMap<SourceA, DestinationA>()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
Mapper.CreateMap<SourceB, DestinationB>()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
I am expecting something like this:
Mapper.CreateMap<DestinationBase>().ForAllSource()
.ForMember(d => d.PropA, opt => opt.Ignore())
.ForMember(d => d.PropB, opt => opt.Ignore())
.ForMember(d => d.PropC, opt => opt.Ignore());
There's now a method FindTypeMapFor which makes this extension method even smaller (and more efficient?):
See the IgnoreAllNonExisting() extension by schdr here:
AutoMapper: "Ignore the rest"?
Usage:
You can ignore all unmapped properties globally. Although this contradict the main benefit of automapper, allows to make explicit mapping only: This is for Automapper 6:
I had the same issue and came across this old question while searching for help. I ended up coming up with the following solution. Maybe it's helpful to someone else...
I have several classes derived from a base class. I want to exclude a property of that base class from all mappings of any class that derives from the base. After creating my mappings (and without specifying any ignore options), I do this:
It's a little brute force because I have to loop through all the type maps, but it gets the job done.
edit: Added a missing { to the if statement