Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Someone Explain This MSDN Code To Me In English?

Tags:

c#

linq

msdn

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);
    }
}
like image 649
sooprise Avatar asked Dec 28 '22 09:12

sooprise


2 Answers

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);
    }
}
like image 146
dcp Avatar answered Jan 12 '23 00:01

dcp


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.

like image 38
Amy B Avatar answered Jan 12 '23 00:01

Amy B