How to undo changes made to all linq tables?

I would like to undo all the changes made to linq tables (this means - I use linq, and the data changes on the client side, the data on the server is not corrupted). How to do it?

EDIT: problem partially resolved

http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/

It works until you use a transaction. When you do and use mixed β€œmode” for recording, the problem arises:

begin trans insert a record update inserted record commit trans 

When you update a record, as described above, Linq counts it as an updated record, and in case of an exception, you have two actions: the transaction is rolled back, and data from Linq are discarded. When canceling changes, Linq tries to retrieve it from the database (a reset for updating means reprogramming the data for writing), but since all changes were discarded, there are no records for updating.

Question

How to improve the DiscardChanges method in a smart general way of dealing with transactions. Or how to change the transaction workflow / drop-submissions so that they all work together?

These are not smart solutions:

  • fetch all data
  • recreation of the connection to the database (because it leads to (1))
+4
source share
2 answers

To add to what Johannes said, I think the confusion here stems from the thought of a DataContext as something similar to a DataSet . This is not true.

A β€œtable” in a DataContext is like a hint on how to get a particular type of data object from a database. Unlike a DataSet , a DataContext does not actually contain β€œdata,” it just keeps track of the discrete objects that you pulled from it. If the DataContext disappears (is deleted), the objects are still valid, they just detach. This is different from a DataSet , where individual DataTables and DataRows are essentially bound to their containers and cannot survive them.

To use the Refresh method for a DataContext , you need to use it in the actual entity or collection of objects. You cannot "Refresh" a Table<T> because it is not an actual physical table, it is just a kind of link.

Changes to objects connected to the DataContext are saved only when the SubmitChanges method is SubmitChanges . If you manage a DataContext , it is absolutely impossible for the changes to be saved unless you manually link individual objects to the new DataContext .

+6
source

Just cancel the current DataContext without calling the SubmitChanges () method and get a new one.

Example:

 DataContext myOldDc = new DataContext(); 
+3
source

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


All Articles