有人可以用英语向我解释这段 MSDN 代码吗?

发布于 2024-09-11 00:59:39 字数 712 浏览 4 评论 0原文

这是与并发相关的。因此 SubmitChanges() 失败,并抛出 ChangeConflictException。对于 db.ChangeConflicts 中的每个 ObjectChangeConflict,其 Resolve 设置为 RefreshMode.OverwriteCurrentValues?这意味着什么?

http://msdn.microsoft.com/en-us/library/bb399354。 ASPX

Northwnd db = new Northwnd("...");
try
{
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}

catch (ChangeConflictException e)
{
    Console.WriteLine(e.Message);
    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
    {
        // All database values overwrite current values.
        occ.Resolve(RefreshMode.OverwriteCurrentValues);
    }
}

This is concurrency related. So the SubmitChanges() fails, and a ChangeConflictException is thrown. For each ObjectChangeConflict in db.ChangeConflicts, its Resolve is set to RefreshMode.OverwriteCurrentValues? What does this mean?

http://msdn.microsoft.com/en-us/library/bb399354.aspx

Northwnd db = new Northwnd("...");
try
{
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}

catch (ChangeConflictException e)
{
    Console.WriteLine(e.Message);
    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
    {
        // All database values overwrite current values.
        occ.Resolve(RefreshMode.OverwriteCurrentValues);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

吃颗糖壮壮胆 2024-09-18 00:59:39

我在代码中添加了一些注释,看看是否有帮助:

Northwnd db = new Northwnd("...");
try
{
    // here we attempt to submit changes for the database
    // The ContinueOnConflict specifies that all updates to the 
    // database should be tried, and that concurrency conflicts
    // should be accumulated and returned at the end of the process.
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}

catch (ChangeConflictException e)
{
    // we got a change conflict, so we need to process it
    Console.WriteLine(e.Message);

    // There may be many change conflicts (if multiple DB tables were
    // affected, for example), so we need to loop over each
    // conflict and resolve it. 
    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
    {
        // To resolve each conflict, we call
        // ObjectChangeConflict.Resolve, and we pass in OverWriteCurrentValues
        // so that the current values will be overwritten with the values
        // from the database
        occ.Resolve(RefreshMode.OverwriteCurrentValues);
    }
}

I added some comments to the code, see if it helps:

Northwnd db = new Northwnd("...");
try
{
    // here we attempt to submit changes for the database
    // The ContinueOnConflict specifies that all updates to the 
    // database should be tried, and that concurrency conflicts
    // should be accumulated and returned at the end of the process.
    db.SubmitChanges(ConflictMode.ContinueOnConflict);
}

catch (ChangeConflictException e)
{
    // we got a change conflict, so we need to process it
    Console.WriteLine(e.Message);

    // There may be many change conflicts (if multiple DB tables were
    // affected, for example), so we need to loop over each
    // conflict and resolve it. 
    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
    {
        // To resolve each conflict, we call
        // ObjectChangeConflict.Resolve, and we pass in OverWriteCurrentValues
        // so that the current values will be overwritten with the values
        // from the database
        occ.Resolve(RefreshMode.OverwriteCurrentValues);
    }
}
夜血缘 2024-09-18 00:59:39

首先,您必须了解 LinqToSql 跟踪每个数据库行的两个状态。原始状态和当前状态。原始状态是数据上下文认为数据库中的状态。当前状态有您的内存中修改。

其次,LinqToSql 使用乐观并发来执行更新。当调用 SubmitChanges 时,数据上下文将原始状态(作为过滤器)与当前状态一起发送到数据库中。如果没有修改任何记录(因为数据库的记录不再与原始状态匹配),则会引发 ChangeConflictException。

第三,要解决更改冲突,您必须覆盖原始状态,以便乐观并发过滤器可以找到记录。然后您必须决定如何处理当前状态...您可以放弃修改(这就是发布的代码所做的),这将导致记录不会发生任何更改,但您已准备好继续使用当前数据库您的应用程序中的值。

First, you must understand that LinqToSql tracks two states for each database row. The original state and the current state. Original state is what the datacontext thinks is in the database. Current state has your in-memory modifications.

Second, LinqToSql uses optimistic concurrency to perform updates. When SubmitChanges is called, the datacontext sends the original state (as a filter) along with the current state into the database. If no records are modified (because the database's record no longer matches the original state), then a ChangeConflictException is raised.

Third, to resolve a change conflict, you must overwrite the original state so the optimistic concurrency filter can locate the record. Then you have to decide what to do with the current state... You can abandon your modifications (that's what the posted code does), which will result in no change to the record, but you are ready to move on with the current database values in your app.

染年凉城似染瑾 2024-09-18 00:59:39

我认为这意味着如果它检测到冲突,请参阅计算科学下的 ,然后进入捕获状态。在其中,它会遍历每个冲突(foreach 循环)并将值重置为尝试发生更改之前的值。

I think it means that if it detects a conflict, see this under computing science, then it goes into the catch. Within there it goes through each of the conflicts (the foreach loop) and resets the values to what they were before the change tried to occur.

烙印 2024-09-18 00:59:39

显然,您对对象所做的任何更改都将被丢弃,因为其他人在您忙碌时抢先一步并更新了数据库。在乐观并发中,放弃更改是唯一可能的自动化解决方案。然而,如果用户花费任何时间来输入丢弃的数据,他们可能不会太高兴。

Apparently, any changes you've done to the objects are to be thrown out, since somebody else has stolen a march on you and updated the database while you were busy. In optimistic concurrency, dropping the changes is the only possible automated solution. However, the user probably isn't going to be too happy if they spent any time on inputting the discarded data.

∞琼窗梦回ˉ 2024-09-18 00:59:39

冲突可能是由于数据上下文中的对象(在.net代码中存储和保存更改等的对象)具有其他值而不是数据库中的值而引起的。

假设您从数据库加载一个人员对象。其中一个字段是名字,名字是 S
哦。现在您在数据上下文中拥有了记录的副本。您更改了一些内容并想要将更改写入数据库,但是当(LINQ?其他orm)想要将更改写入数据库时​​,它注意到数据库中的名字已经更改。

因此,某人/某事改变了您的记录,您遇到了某种“僵局”(正确的术语?),那么您必须定义什么更重要,您的更改或其他人/某事所做的更改。

说到重点!!! -> Refreshmode.overwirteCurrentValues 只是刷新数据上下文中的对象,它会从数据库重新加载对象,以便您可以使用更新的对象。

我希望这有点清楚:)

grtz

The conflict is propabely caused by the fact that the object in your datacontext (the object that stores and keeps changes etc in .net code) has other values then the ones in your db.

Let's say you load a person object from the db. One of the fields is firstname, firstname is S
oo. Now you have a copy of your record in the datacontext. You change some things and want to write the changes to the db, but when (LINQ? other orm) wants to write the changes to the DB, it notices that the firstname in the DB is already changed.

So someone/something has changed your record, you have kind of a "deadlock" (correct term?) then you have to define what is more important, your changes, or the changes that something/someone else made.

TO THE POINT !!! -> Refreshmode.overwirteCurrentValues Just refreshes the object in your datacontext, it RELOADS the object from the db so that you are working with the updated object.

I hope this was a little clear :)

grtz

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