我不明白为什么它会覆盖我的整个对象。原因是我User
从 db 获取我的对象,我想从 DTO 分配新值。它不仅仅是添加那些新值,而是创建具有新值但所有先前设置为的新对象null
。
我如何确保在这种情况下他会“升级”我的对象,而不是创建新对象?
设想
/users/{id}
- 放
// User has id, username, fullname
// UserPut has fullname
public HttpResponseMessage Put(int id, UserPut userPut)
{
var user = _db.Users.SingleOrDefault(x => x.Id == id); // filled with properties
Mapper.CreateMap<UserPut, User>();
user = Mapper.Map<User>(userPut); // now it has only "fullname", everything else set to null
// I can't save it to db because everything is set to null except "fullname"
return Request.CreateResponse(HttpStatusCode.OK, user);
}