1

It seems that nullable type properties in source is ignored and not copied to destination.

Consider these classes:

public class Source 
{
    public int? Test { get; set; }
}

public class Destination 
{
    public int? Test { get; set; }
}

Mapping:

Mapper.CreateMap<Source, Destination>();

var source = new Source() { Test = 1 };
var destination = new Destination();

Mapper.Map<Source, Destination>(source, destination);

Assert.AreEqual(source.Test, destination.Test); //true

source.Test = null;

Mapper.Map<Source, Destination>(source, destination);

Assert.AreEqual(source.Test, destination.Test); //false (null, 1)

It worked when I used:

Mapper.CreateMap<Source, Destination>()
    .ForMember(m => m.Test, o => o.ResolveUsing(m => m.Test));

But I don't want to set that per property, can you set that globally? Or is there any other way to achieve this?

mollwe
  • 2,185
  • 2
  • 18
  • 17

1 Answers1

2

Seems that creating maps for each nullable type to itself like:

Mapper.CreateMap<int?, int?>()
    .ConvertUsing(v => v);

Seems to work as a fix. But I would rather have a complete solution with all nullable types then specifiyng them one by one. Easy to miss something.

mollwe
  • 2,185
  • 2
  • 18
  • 17