c# - Do I have to refresh a LINQ object that represents a view/table row after I do a submitchanges operation that may alter it? -


Do I need to refresh a linux object which represents the view / table row after a subcategory operation that changes it Could?

Assume that I have

  event _event = new event {someproperty = "this"; }; DataContext.Events.InsertOnSubmit (_Event); DataContext.SubmitChanges (); // Some operations _event.someproperty = "that"; DataContext.SubmitChanges ();  

Adds to the equation that during some operations, a different thread, with a different example, can change the same identity of DataContext and store it.

I sometimes get an exception saying that the row can not be found or changed. Do not I have to search again around me?

I think that you use optimists, concurrency with linkak means that when in db If an object is updated, the update statement checks that the row was not changed in the meantime.

If any other process has changed the object in the DB between your initial reading (or creation), the object and your update, the link will throw a ChangeConflictException you catch this exception And can struggle (by reloading the object from DB or by overwriting the DB values).

More details here:

Your code might look something like this:

Try {_event.someproperty = "that"; DataContext.SubmitChanges (); } Hold (ChangeConflictException E) {foreach (ObjectChangeConflict occ) in db.ChangeConflicts {// All database values ​​overwrite current values ​​occ.Resolve (RefreshMode.OverwriteCurrentValues); }}

There are several refresh mode and it really depends on how you want to handle your application. Please find more details in these answers:


Comments