When you map with
var model = Mapper.Map<User>(updateUser);
Then new instance of destination User
object is created and populated with data from updateUser
. You should map from source object to existing object instead:
var existUser = _uow.Users.GetById(updateUser.UserId);
var model = Mapper.Map(updateUser, existUser);
// you even don't need model here
// just call Mapper.Map(updateUser, existUser) and use existUser
Thus AutoMapper
will use existing instance of User
and it will update it with data from UpdateUserViewModel
.
NOTE: It's better to create mappings once on application startup.