I have a problem with mapping collections of the same type objects with AutoMapper.
Let me give you an example :
First, object classes:
public class ClassA
{
public string Name { get; set; }
public string Type { get; set; }
}
public class ClassB
{
public string Name { get; set; }
public List<ClassA> Classes { get; set; }
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<ClassA, ClassA>()
.ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name))
.ForAllOtherMembers(opts => opts.Ignore());
CreateMap<ClassB, ClassB>()
.ForMember(dest => dest.Name, opts => opts.MapFrom(src => src.Name))
.ForMember(dest => dest.Classes, opts => opts.MapFrom(src => src.Classes))
.ForAllOtherMembers(opts => opts.Ignore());
}
}
And now when executing such code:
List<ClassB> targetList;
targetList = DbContext.ClassesB.ProjectTo<ClassB>(Mapper.ConfigurationProvider).ToList();
Mapping does not work correctly. ClassB.Name is mapped correctly, but it looks like that mapping definition for ClassA is ignored, because all properties are mapped. Additionally, when I change ClassB.Classes property to no-list (ClassA) mapping work correctly.
Is it AutoMapper's error that it is ignoring defined mappings?