How is the LINQ to SQL related transaction rolled back?

The question is only to undo the changes, not commit.

Let's say I take some data, I change it, I send the changes (optional step) and roll back the transaction. Wherever you look at each author, it discards the changes.

But I found out that this is half the truth - LINQ DataContext will save the changed data! I tested this with TransactionScope and DataContext.Transaction. In both cases, I got the same behavior.

A workaround would be to recreate the DataContext after the rollback (however, this leads to other problems, such as caching data and handling nested transactions) or manually discarding changes to the DataContext. However, these are just workarounds.

Questions

So what am I missing? LINQ to SQL is not suitable for transactions? How to use transactions so that they REALLY revert the changes?

Example

                MyTable record = null;

                db.Connection.Open();
                using (db.Transaction = db.Connection.BeginTransaction())
                {
                        record = db.MyTable.First();
                        record.BoolField = !record.BoolField; // changed
                        db.SubmitChanges();
                        db.Transaction.Rollback();
                }
+5
source share
3 answers

The data context should be considered as a whole. How much detail you do this is up to you - it could be a page request or one operation; but - if you get an exception (or almost nothing unexpected) - stop ; abandon data context and rollback. After the rollback, your data context will be confused, so just don't store it.

; , . .

+4

, , , , - ( - ), ORM. , LINQ to SQL ORM, . , , Unit of Work. , , . , , . , , -, , , .

+2

:

1) datacontext

, , "" . datacontext rollbak. , . datacontext. ! ( )

2)

. Linq2Sql Submitchanges. (, datacontexts linq2sql, TransactionScope. .Complete() , .

0

All Articles