'MapFrom not being called for null collection
I want to upgrade AutoMapper library in my project from v4.2.1 to the latest that still supports .NET Framework 4.5.2 (which to my knowledge is AutoMapper 7.0.1). I have code like this:
Mapper.Initialize(cfg =>
{
cfg.AllowNullCollections = true;
cfg.CreateMap<ExampleDto, Example>(MemberList.Source)
.ForMember(dest => dest.ItemsAAA, o => o.MapFrom(x => x.items.ConvertTo<TypeAAA>()))
.ForMember(dest => dest.ItemsBBB, o => o.MapFrom(x => x.items.ConvertTo<TypeBBB>()));
});
ExampleDto.items is a collection that should be split to Example.ItemsAAA and Example.ItemsBBB based on every item type. ConvertTo is an extension method, and it returns an empty collection if input parameter is null. This used to work in AutoMapper 4.2.1, but in 7.0.1 ConvertTo is never called for x.items == null. How can I make AutoMapper call this mapping even if the x.items is null?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
