Automapper UseDestinationValue

Having a display problem

VPerson vPerson = new VPerson() { Id = 2, Lastname = "Hansen1", Name = "Morten1" };
DPerson dPerson = new DPerson() { Id = 1, Lastname = "Hansen", Name = "Morten" };

Mapper.Initialize(x =>
{
     //x.AllowNullDestinationValues = true; // does exactly what it says (false by default)
});

Mapper.CreateMap();

Mapper.CreateMap()
      .ForMember(dest => dest.Id, opt => opt.UseDestinationValue());

Mapper.AssertConfigurationIsValid();

dPerson = Mapper.Map<VPerson, DPerson>(vPerson);

dPerson is 0, I think it should be 1, or am I missing something?

Working example

VPerson vPerson = new VPerson() { Id = 2, Lastname = "Hansen1", Name = "Morten1" };
        DPerson dPerson = new DPerson() { Id = 1, Lastname = "Hansen", Name = "Morten" };

        Mapper.Initialize(x =>
        {
            //x.AllowNullDestinationValues = true; // does exactly what it says (false by default)
        });

        Mapper.CreateMap<DPerson, VPerson>();

        Mapper.CreateMap<VPerson, DPerson>()
            .ForMember(dest => dest.Id, opt => opt.UseDestinationValue());


        Mapper.AssertConfigurationIsValid();

        dPerson = Mapper.Map(vPerson, dPerson);
+3
source share
1 answer

I never used the UseDestinationValue () parameter, but it looks like you just want to NOT display the identifier when switching from VPerson to DPerson. If so, use the Ignore option:

.ForMember(d => d.Id, o => o.Ignore());

EDIT

Oh shoot - I didn’t even notice the syntax you used. You need to use the Map overload, which accepts an existing destination:

Mapper.Map(vPerson, dPerson);

DPerson, . , , dPerson, ( Ignore, , Id ).

+6

All Articles