![](/img/trans.png)
[英]SubmitChanges not updating, but inserts new record. LINQ to SQL
[英]LINQ: SubmitChanges() not updating my record
这里听起来不像是破记录(有几篇帖子看起来像这样),但似乎没有一个解决我的问题。 看来,当您要更新时
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
将其拉出。
您应该设置公共属性,而不是私有值。
// 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将不会跟踪更改。
DJ,
你确定吗
user._EncryptedPassword ,
user._PasswordSalt
是属性吗? 我认为您LINQ TO SQL创建公共和私有属性。
你可以设置它们吗
user.EncryptedPassword ,
user.PasswordSalt
像这样 ?
维德
要对代码进行故障排除,请尝试以下任何建议:
Ved指出了一个可能的问题。 万一这行不通,您应该仔细检查LINQ to SQL类的AdminUser
类定义,并确保生成的代码实现INotifyPropertyChanging
和INotifyPropertyChanged
接口。 在某些情况下,设计人员将不会实现这些接口,从而阻止更新工作。 例如,不声明主键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.