364

How can I use automapper to update the properties values of another object without creating a new one?

4 Answers 4

627

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.

Sign up to request clarification or add additional context in comments.

15 Comments

Thanks, 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.
Automapper 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.
Not 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);
FYI, I discovered you don't need to specify the type parameters. Simply, Mapper.Map(source, destination) will work. Nice and simple!
@Péter, I have reproduced it but corrected by adding cfg.CreateMap<User, User>();
|
45

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>()

4 Comments

Ha! 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 :)
This 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.
This was the issue for me, too. Had to create a profile that maps A to A and then it worked.
Very important, this. I couldn't figure out why mapper.Map(source, destination) wasn't updating the destination object. My understanding was that AutoMapper would automatically create an implicit mapping between objects of the same type. I read this answer and tried adding an explicit mapping from the type to itself (similar to the mapping of User to User in this answer) and now it works.
16

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.

Here's the relevant source code

Comments

2

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);

1 Comment

I think the static mapper was depreciated?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.