How can I use automapper to update the properties values of another object without creating a new one?
292
votes
3 Answers
508
votes
26
votes
6
votes
If you wish to use an instance method of IMapper, rather than the static method used in the accepted answer, you can do the following (tested in AutoMapper 6.2.2
)
IMapper _mapper;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>();
});
_mapper = config.CreateMapper();
Source src = new Source
{
//initialize properties
}
Destination dest = new dest
{
//initialize properties
}
_mapper.Map(src, dest);
dest
will now be updated with all the property values from src
that it shared. The values of its unique properties will remain the same.