Automapper: Error creating abstract member that should be ignored

Created on 18 Jul 2017  路  3Comments  路  Source: AutoMapper/AutoMapper

Source/destination types

public abstract class BaseClass
{
    public Guid ID { get; set; }
}

public class Thing
{
    public BaseClass Entity { get; set; }
}

public class ThingDto
{
    public Guid EntityID { get; set; }
}

Mapping configuration

public class ThingProfile : Profile
{
    public ThingProfile()
    {
        CreateMap<Thing, ThingDto>()
            .ReverseMap()
            .ForMember(dest => dest.Entity, conf => conf.Ignore());
    }
}

Version: 6.1.1

Expected behavior

We don't want Entity to be mapped at this stage, it stays null and we map it later in parent mapping. We didn't have any issues with this scenario in older versions.

Actual behavior

ArgumentException - "Cannot create an instance of abstract type BaseClass".

Steps to reproduce

Mapper.Initialize(x =>
    {
        x.AddProfile(new ThingProfile());
    });
Mapper.Map<Thing>(new ThingDto());

Most helpful comment

MapFrom-s (including the default unflattening) are reversed now. You can drop ReverseMap and create the maps or ignore the offending path.
C# CreateMap<Thing, ThingDto>() .ReverseMap() .ForPath(d=>d.Entity.ID, o=>o.Ignore()));

All 3 comments

MapFrom-s (including the default unflattening) are reversed now. You can drop ReverseMap and create the maps or ignore the offending path.
C# CreateMap<Thing, ThingDto>() .ReverseMap() .ForPath(d=>d.Entity.ID, o=>o.Ignore()));

See also #2199.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings