How can I use automapper to update the properties values of another object without creating a new one?
4 Answers
Use the overload that takes the existing destination:
Mapper.Map<Source, Destination>(source, destination);
Yes, it returns the destination object, but that's just for some other obscure scenarios. It's the same object.

- 26,045
- 5
- 74
- 69
-
38Thanks, Jimmy...I purposely stayed away from AutoMapper because I was afraid of the learning curve impacting my schedule. I'm officially sorry I stayed away so long...it's much easier than I initially thought. – Neil T. Apr 08 '10 at 23:26
-
22Automapper has to be one of the most useful libraries out there. I too avoided it for a long time, much too long. I started using it today and am very surprised at how easy it is to use. – mcottingham Mar 09 '13 at 03:59
-
6Not work for me. I try to map to same type, but it's return new object. User u1, u3 = new User(); u1 = new User { Id = 1, NickName = "vami" }; User u4 = Mapper.Map(u1, u3); Assert.AreEqual(u1.Id, u3.Id); Assert.AreEqual(u1.NickName, u3.NickName); – Péter Aug 26 '13 at 12:33
-
This does not work for me. I get an error asking for constructor but it should not need one if it is mapping to an existing object... See this article. http://stackoverflow.com/questions/20099405/automapper-not-copying-from-source-to-destination-object-correctly-errors-nee# – retslig Nov 25 '13 at 19:20
-
8FYI, I discovered you don't need to specify the type parameters. Simply, Mapper.Map(source, destination) will work. Nice and simple! – Judah Gabriel Himango Feb 13 '15 at 16:02
-
2Has the NULL behaviour changed in release 4.2.1? I'm seeing that mapping null into an object returns null; whereas in 2.2.1, mapping null into an existing object returned the existing object. – Peter McEvoy Mar 14 '16 at 16:14
-
@PeterMcEvoy maybe. I have hundreds of tests but I can't cover every scenario. – Jimmy Bogard Mar 15 '16 at 14:28
-
1@Péter, I have reproduced it but corrected by adding cfg.CreateMap
(); – Andrzej Martyna Oct 25 '17 at 14:50 -
@JimmyBogard, Peter's issue I have corrected by using "self-refering" CreateMap. BTW. I was used to have exception in situation that I don't have a mapping configured but this time AutoMapper silently does nothing. – Andrzej Martyna Oct 25 '17 at 14:53
-
2@AndrzejMartyna hmm, I was trying to do that with `IEnumerable<>`, but I just noticed the **self-refering** solution only works for individual entities. Well, a `foreach` solved my situation, but I wonder why using `IEnumerable<>` doesn't work. – Alisson Reinaldo Silva Jan 04 '18 at 13:42
-
@JimmyBogard How can I propagate this behavior down to the properties being mapped? For example, Say I have a class Foo with a reference to a class Bar. I want the fields of Foo.Bar to be updated with the values of the source's Foo.Bar. Is there any way to achieve this? – Raikol Amaro Jul 23 '19 at 14:46
-
Sorry, it doesn't work. It creates a new object. In my case I obtain an entity from the db, then apply a model to it. It overwrites all the field with defaults. In my case the model doesn't have a date value where as the entity does. When I map model->entity, the date goes to it's default value. – code5 Feb 26 '20 at 19:13
-
1This could be simplified to: `Mapper.Map(source, destination)` – Offir Jun 21 '20 at 12:19
-
Hi guys, This solution works for main object, but if this main object has nested objects we will have the same problem. .MapFrom() will be overwritte the nested object).Does anyone know how to avoid this same problem in nested objects? – Pikachuuuu Mar 09 '23 at 18:15
-
https://stackoverflow.com/questions/75688688/autommaper-overwrite-the-value-of-nested-object – Pikachuuuu Mar 09 '23 at 18:30
To make this work you have to CreateMap for types of source and destination even they are same type.
That means if you want to
Mapper.Map<User, User>(user1, user2);
You need to create map like this
Mapper.Create<User, User>()

- 410
- 4
- 4
-
2Ha! I've just found the same and added a comment to the accepted answer. IMHO the accepted answer without your explanation is not complete but after my comment it might be, finally :) – Andrzej Martyna Oct 25 '17 at 14:59
-
1This is a great addition. However, the original question did not mention the types of the source and destination, and did not require that they be the same type. – jpaugh Feb 07 '18 at 18:03
-
This was the issue for me, too. Had to create a profile that maps A to A and then it worked. – Jens Mander Jul 03 '21 at 17:50
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.

- 2,090
- 23
- 41
There's two things to note here. First, we don't have to specify the type to map to for the generic Map call. This is because, now, we're passing the destination object instance, so the destination type can be determined by the type of that object. Second, we're not storing the result of this call in a variable. This is because the destination object is mapped to in place and we're not creating any new instances.
AutoMapper.Mapper.Map(sourceObject, destinationObject);

- 380
- 5
- 14