سؤال

I'm facing a problem with AutoMapper 2.1.267.0 when working with objects containing collections of derived classes. I've isolated 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 diagram summarizing everything (Red arrows represent AutoMapper mappings)

enter image description here

I've mapped a ContainerA instance to ContainerB. This first scenario works properly. The destination object is fully filled as shown in the image bellow:

enter image description here


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>();
    });

enter image description here

the result is

enter image description here

The "Text" properties of all elements of the collection become null. As you can see in the image, the "Text" property of the containerB.ClassB object is still mapped correctly. This only occurs with collections. I don't know what's happening. Any clues?

Thanks

هل كانت مفيدة؟

المحلول

Hi,

Try to change the mapping between containers in this way :

  public class ContainerAToContainerB : Profile
    {
        protected override void Configure()
        {
            CreateMap<ContainerA, ContainerB>()
                .ForMember(dest => dest.ClassBList,opt=>opt.MapFrom(src=>src.ClassAList))
                .ForMember(dest => dest.ClassB, opt => opt.MapFrom(src => src.ClassA));

        }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top