LINQ:SubmitChanges() 没有更新我的记录
听起来不像是一张破唱片(有一些帖子看起来像这个),但它们似乎都没有解决我的问题。看来,当您想要更新时,
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
来获取它。
Not to sound like a broken record here (there a few posts that look like this one) but none of them seem to solve my problem. It seems that when you want to update
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;
}
}
Maybe this a dumb question but I'm retrieving this
from the db... why not just update this
's properties. I'm guess I need to pull it through the DBContext
I guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该设置公共属性而不是私有值。
这不会触发任何更新。
即使您这样做:
这也不会触发任何更改,因为您实际上并未更改值
您可以执行类似的操作“
同时检查您的表是否有主键”,否则 Linq 将不会跟踪更改。
You should be setting the public properties and not the private values.
This won't trigger any updates.
Even if you do :
this won't trigger any change either as you are not actually changing the values
You can do something like
Also check that your table has a primary key, otherwise Linq will not track the changes.
DJ,
你确定
是属性吗?我认为 LINQ TO SQL 创建了公共和私有属性。
你能
像这样设置它们吗?
韦德
DJ,
Are you sure
are the properties ? I think you LINQ TO SQL creates public and private properties.
Can you set them
like this ?
Ved
要对代码进行故障排除,请尝试以下任何建议:
To troubleshoot your code, try any of these suggestions:
维德指出了一个可能的问题。万一这不起作用,您应该仔细检查 LINQ to SQL 类的
AdminUser
类定义,并确保生成的代码实现INotifyPropertyChanging
和INotifyPropertyChanged 接口。在某些情况下,设计者不会实现这些接口,从而阻止更新工作。例如,不声明主键。
Ved pointed out one possible problem. Just in case that doesn't work, you should double check your LINQ to SQL class'
AdminUser
class definition and make sure that the generated code implements theINotifyPropertyChanging
andINotifyPropertyChanged
interfaces. There are some cases where the designer will not implement these interfaces which prevents updates from working. e.g., not declaring a primary key.