gpt4 book ai didi

c# - LINQ:SubmitChanges() 没有更新我的记录

转载 作者:行者123 更新时间:2023-11-30 20:10:32 25 4
gpt4 key购买 nike

在这里听起来不像是破记录(有一些帖子看起来像这个),但它们似乎都没有解决我的问题。好像要更新的时候

private bool resetPassword(string password)
{
try
{
var db = new SchedulerDBDataContext();

// since this is a instance method, I grab the ID from _this_
AdminUser user = db.AdminUsers.SingleOrDefault(t => t.ID == _ID);

if (user != null)
{
// this method DOES update these two fields.
SchedUtil.md5Hash(password, ref user._EncryptedPassword, ref user._PasswordSalt);

// I threw these in there to try something... it didn't work.
//user._EncryptedPassword = user.EncryptedPassword;
//user._PasswordSalt = user.PasswordSalt;

// this DOESN'T do anything.
db.SubmitChanges();
return true;
}

return false;
}
catch (Exception)
{
return false;
}
}

也许这是个愚蠢的问题,但我正在从数据库中检索 this... 为什么不更新 this 的属性。我想我需要通过 DBContext 拉它。

最佳答案

您应该设置公共(public)属性而不是私有(private)值。

  // I threw these in there to try something... it didn't work.
//user._EncryptedPassword = user.EncryptedPassword;
//user._PasswordSalt = user.PasswordSalt;

这不会触发任何更新。

即使你这样做了:

      user.EncryptedPassword = user._EncryptedPassword;
user.PasswordSalt = user._PasswordSalt;

这也不会触发任何更改,因为您实际上并没有更改值

你可以做类似的事情

 string newEncryptedPassword;
string newPasswordSalt;

SchedUtil.md5Hash(password, ref newEncryptedPassword, ref newPasswordSalt);

user.EncryptedPassword = newEncryptedPassword;
user.PasswordSalt = newPasswordSalt;

还要检查您的表是否有主键,否则 Linq 将不会跟踪更改。

关于c# - LINQ:SubmitChanges() 没有更新我的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904806/

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