gpt4 book ai didi

c# - AutoMapper - 为什么要覆盖整个对象?

转载 作者:行者123 更新时间:2023-11-30 13:58:09 25 4
gpt4 key购买 nike

<分区>

我不明白为什么它会覆盖我的整个对象。原因是我从 db 获取了我的 User 对象,我想从 DTO 分配新值。它不是仅仅添加这些新值,而是创建具有新值但所有先前值都设置为 null 的新对象。

我如何确保在这种情况下他会“升级”我的对象,而不是创建新对象?

场景

/users/{id} - PUT

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com