更新一个对象中的值 ASP.Net 标识 2.0 更新第二个对象值



我正在根据id获取这2个对象,Id是相同的

var oldeUserExist = await UserManager.FindByIdAsync(model.UserData.Id ?? null);
var userExist = await UserManager.FindByIdAsync(model.UserData.Id ?? null);

问题是什么

->考虑oldeUserExist.phonenumber为空

->如果我更新用户存在对象中的电话号码值,它会更新旧用户存在对象中的电话号码值,这也会导致问题

我的问题是 FindByIdAsync 正在通过引用对象提供副本? 如果是,那么我们应该怎么做才能避免这种情况?

谢谢

只需使用以下代码创建 User 对象的克隆:

public static T Clone<T>(T toClone) where T : class
{
string tmp = Newtonsoft.Json.JsonConvert.SerializeObject(toClone,Formatting.None, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
});
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(tmp);
}

这样,这两个对象就不会相互引用。

最新更新