I have a problem with AutoMapper 2.1.267.0 when working with objects containing collections of derived classes. I highlighted my problem in a simpler scenario with the following classes:
public class ClassABase
{
public string Name { get; set; }
}
public class ClassA : ClassABase
{
public string Description { get; set; }
}
public class ClassBBase
{
public string Title { get; set; }
}
public class ClassB : ClassBBase
{
public string Text { get; set; }
}
public class ContainerA
{
public IList<ClassA> ClassAList { get; set; }
public ClassA ClassA { get; set; }
}
public class ContainerB
{
public IList<ClassB> ClassBList { get; set; }
public ClassB ClassB { get; set; }
}
and these mappings
public class ClassABaseToClassBBase : Profile
{
protected override void Configure()
{
CreateMap<ClassABase, ClassBBase>()
.Include<ClassA, ClassB>()
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Name));
Mapper.AssertConfigurationIsValid();
}
}
public class ClassAToClassB : Profile
{
protected override void Configure()
{
CreateMap<ClassA, ClassB>()
.ForMember(dest => dest.Text, opt => opt.MapFrom(src => src.Description));
Mapper.AssertConfigurationIsValid();
}
}
public class ContainerAToContainerB : Profile
{
protected override void Configure()
{
CreateMap<ContainerA, ContainerB>()
.ForMember(dest => dest.ClassBList,
opt => opt.MapFrom(src => Mapper.Map<IList<ClassA>, IList<ClassB>>(src.ClassAList)))
.ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));
}
}
Here is the initialization
Mapper.Initialize(x =>
{
x.AddProfile<ClassABaseToClassBBase>();
x.AddProfile<ClassAToClassB>();
x.AddProfile<ContainerAToContainerB>();
});
and a chart summarizing everything (the red arrows are AutoMapper mappings)

I have mapped an instance of ContainerA to a container. This first scenario is working correctly. The destination is fully populated as shown in the image below:

But if I add this mapping
public class ClassBBaseToClassB : Profile
{
protected override void Configure()
{
CreateMap<ClassBBase, ClassB>()
.ForMember(dest => dest.Text, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
}
}
Mapper.Initialize(x =>
{
x.AddProfile<ClassABaseToClassBBase>();
x.AddProfile<ClassAToClassB>();
x.AddProfile<ClassBBaseToClassB>();
x.AddProfile<ContainerAToContainerB>();
});

result

"" . , "" containerB.ClassB - . . , . ?