Reputation: 29427
I have the following abstract classes
public abstract class BaseClass
{
public int NumberProperty { get; set; }
}
public abstract class BaseClass_DTO
{
public int NumberProperty { get; set; }
public string NumberPropertyAsString { get; set; }
}
and the following concrete class
public class ConcreteA : BaseClass
{
public string StringProperty { get; set; }
}
public class ConcreteA_DTO : BaseClass_DTO
{
public string StringProperty { get; set; }
}
And this is the Map
Mapper.Initialize( cfg => {
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember( p => p.NumberPropertyAsString, opt => opt.MapFrom( x => x.NumberProperty.ToString() ) );
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap();
} );
Mapper.AssertConfigurationIsValid();
The code says that the configuration is not valid because of
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ConcreteA_DTO -> ConcreteA (Source member list)
Temporary.Test.ConcreteA_DTO -> Temporary.Test.ConcreteA (Source member list)
Unmapped properties:
NumberPropertyAsString
The error happens because of the ReverseMap()
call. How do I set the reverse map to ignore the member without duplicating the Map?
Upvotes: 1
Views: 691
Reputation: 13234
You will need to ignore the NumberPropertyAsString
in the reverse map using
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
It appears that in your specific mapping config, you should make the configuration both in the base and the concrete as well.
1. In the base class map.
cfg.CreateMap<BaseClass, BaseClass_DTO>()
.ForMember(p => p.NumberPropertyAsString, opt => opt.MapFrom(x => x.NumberProperty.ToString()))
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
2. In the concrete class map.
cfg.CreateMap<ConcreteA, ConcreteA_DTO>()
.IncludeBase<BaseClass, BaseClass_DTO>()
.ReverseMap()
.ForSourceMember(p => p.NumberPropertyAsString, opt => opt.Ignore());
Upvotes: 1