Can someone explain this MSDN code to me in English?

This is concurrency related. Thus, the SubmitChanges () method fails, and a ChangeConflictException is thrown. For each ObjectChangeConflict object in db.ChangeConflicts, its Resolve is set to RefreshMode.OverwriteCurrentValues? What does it 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); } } 
+4
source share
5 answers

I added some comments to the code, let's 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); } } 
+9
source

First, you must understand that LinqToSql keeps track of two states for each row in the database. Initial state and current state. The initial state is what the datacontext in the database thinks. The current state has its modifications in memory.

Secondly, LinqToSql uses optimistic concurrency to perform updates. When the SubmitChanges function is called, the datacontext sends the original state (as a filter) along with the current state to the database. If the records do not change (because the database record no longer matches the initial state), a ChangeConflictException is thrown.

Third, in order to resolve the conflict of changes, you must overwrite the original state so that the optimistic concurrency filter can find the record. Then you have to decide what to do with the current state ... You can abandon your modifications (this is what the published code did), which will not lead to a change in the record, but you are ready to go with the current database of values ​​in your application.

+1
source

I think this means that if he finds a conflict, see this in the field of computational science, then he goes into the catch. Inside, it goes through each of the conflicts (foreach loop) and resets the values ​​to what they were before the change occurred.

0
source

Apparently, any changes you made to the objects should be thrown away, as someone else stole you a march and updated the database while you were busy. In optimistic concurrency, discarding changes is the only automated solution possible. However, the user will probably not be too happy if they take some time to enter the discarded data.

0
source

The conflict is due to the fact that the object in your datacontext (the object that stores and saves changes, etc. in the .net code) has different values ​​than those in your db.

Say you are loading a person object from db. One of the fields is firstname, firstname is S oo. You now have a copy of your record in the datacontext. You change some things and want to write changes to db, but when (LINQ? Other orm) wants to write changes to the database, he notices that the first name in the database is already changed.

So, someone has changed your entry, you have a β€œdead end” (correct term?), Then you must determine, more importantly, your changes or the changes that something made. p>

EXACTLY!!! β†’ Refreshmode.overwirteCurrentValues ​​Just updates the object in your datacontext, it is a RELOADS object from db, so you are working with the updated object.

Hope this was a little clear :)

grtz

0
source

Source: https://habr.com/ru/post/1316471/


All Articles