Automapper does not populate destination
I use Automapper
with the option CreateMissingTypeMaps
set to true
. If I try to populate an existing object of the same type, it doesn't work.
class A
{
public string X { get; set; }
}
var config = new MapperConfiguration(cfg => cfg.CreateMissingTypeMaps = true);
var mapper = config.CreateMapper();
var a1 = new A { X = "sample" };
var a2 = new A();
mapper.Map(a1, a2); // a2.X was not set
If I create a new object of the same type it works fine
var a3 = mapper.Map<A>(a1); // a3.X is set
If I fill an existing object of a different type, it also works
class B
{
public string X { get; set; }
}
var b = new B();
mapper.Map(a1, b); // b.X is set
But if I try to populate an existing object of the same type, it doesn't. Is this a bug in Automapper
or am I missing something?
source to share
For some reason this is the expected behavior https://github.com/AutoMapper/AutoMapper/issues/2129 . CreateMissingTypeMaps
not supported for matching with the same type. The only way to get this to work is to explicitly customize the display:
var config = new MapperConfiguration(cfg => cfg.CreateMap<A, A>());
source to share