0
votes

Is there a provision in Automapper to ignore certain properties while mapping. For example, I have two classes Manager and Employee. Manager has a list of employees and other information.

I need employees list in Manager most of the times but in few cases I do not need employees list to be returned to the client (say while just editing manager names). So, when I create map, I included Employees to be mapped too. Now is there a way to specify employees property to be ignored at the time of mapping.

// <--- Employees is included.
Mapper.CreateMap<Manager, ManagerDto>();

// <--- I want to ignore employees list here.
ManagerDto dto = Mapper.Map<Manager, ManagerDto>(manager); 
1

1 Answers

1
votes

You could possibly use conditions in your mapping configuration. For example:

Mapper.CreateMap<Manager, ManagerDto>()
    .ForMember(d => d.Employees,
               opt => {
                   opt.Condition(s => s.NeedEmployees);
                   opt.MapFrom(s => s.Employees);
               });

I don't believe you can do it at the time you're actually applying the mapping.