csharp AutoMapper: Update property values without creating a new object

Dec 28, 2016 00:09 · 55 words · 1 minute read

Normally, we use AutoMapper to Map object like this:

Mapper.Initialize(
       (cfg) =>
        {
            cfg.CreateMap<TSource, TTarget>();
        }
    );
target = Mapper.Map<TTarget>(source);

however, this will create a new instanse of TTarget, then the reference of target would be changed. The original object of target will not change in this case. The solution is as follows:

Mapper.Map(source, target);