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

发布于 2024-10-16 03:14:46 字数 1019 浏览 3 评论 0原文

听起来不像是一张破唱片(有一些帖子看起来像这个),但它们似乎都没有解决我的问题。看来,当您想要更新时,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

木緿 2024-10-23 03:14:46

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

  // 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 将不会跟踪更改。

You should be setting the public properties and not the private values.

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

This won't trigger any updates.

Even if you do :

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

this won't trigger any change either as you are not actually changing the values

You can do something like

 string newEncryptedPassword;
 string newPasswordSalt;

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

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

Also check that your table has a primary key, otherwise Linq will not track the changes.

青芜 2024-10-23 03:14:46

DJ,

你确定

user._EncryptedPassword , 
user._PasswordSalt 

是属性吗?我认为 LINQ TO SQL 创建了公共和私有属性。

你能

user.EncryptedPassword , 
user.PasswordSalt

像这样设置它们吗?

韦德

DJ,

Are you sure

user._EncryptedPassword , 
user._PasswordSalt 

are the properties ? I think you LINQ TO SQL creates public and private properties.

Can you set them

user.EncryptedPassword , 
user.PasswordSalt

like this ?

Ved

凝望流年 2024-10-23 03:14:46

要对代码进行故障排除,请尝试以下任何建议:

  • 在调试代码时,我将假设您的对象不为空。
  • 确保您的属性确实发生了变化。奇怪的是,您使用管道前缀的字段名称,但无论如何:在调试时,检查您的属性实际上是否具有新值。
  • 使用 SQL Server Profiler 捕获发送到数据库服务器的 SQL 语句。然后,您将能够在 SQL Management Studio 中重新运行此 UPDATE 查询,并确定有多少记录受到影响。您还可以看到 UPDATE 语句中传递的值。

To troubleshoot your code, try any of these suggestions:

  • while debugging the code, I'll assume that your object is not null.
  • ensure that your properties are actually changed. It's odd that you're using pipe-prefixed field names, but either way: while debugging, check that your properties actually have new values.
  • use SQL Server Profiler to capture the SQL statement sent to the database server. You'll then be able to re-run this UPDATE query back into SQL Management Studio, and determine how many records are effected. You'll also be able to see the values passed in the UPDATE statement.
美人迟暮 2024-10-23 03:14:46

维德指出了一个可能的问题。万一这不起作用,您应该仔细检查 LINQ to SQL 类的 AdminUser 类定义,并确保生成的代码实现 INotifyPropertyChangingINotifyPropertyChanged 接口。在某些情况下,设计者不会实现这些接口,从而阻止更新工作。例如,不声明主键。

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 the INotifyPropertyChanging and INotifyPropertyChanged 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文