Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbContextTransaction Rollback

Entity Framework 6 introduced a new way to support transactions in the DbContext with the BeginTransaction method:

var db = new MyDbContext(); using(var tx = db.Database.BeginTransaction()) {     // update entities     try     {         db.SaveChanges();         tx.Commit();     }     catch(Exception)     {         tx.Rollback();     } } 

Is the Rollback() call in the method necessary? What happens if it is not called on an exception? I know when using TransactionScope it will roll back the transaction automatically when it is disposed and Complete is not called. Does DbContextTransaction behave similarly?

like image 829
Dismissile Avatar asked Apr 07 '14 12:04

Dismissile


People also ask

How do I rollback Entity Framework?

A DbContextTransaction object provides Commit() and Rollback() methods to do commit and rollback on the underlying store transaction. This method requires an open underlying stored connection. This method opens a connection if it is not already open. This method will close the connection when Dispose () is called.

Is SaveChanges a transaction?

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.

Does EF core use transaction?

EF Core relies on database providers to implement support for System. Transactions. If a provider does not implement support for System.

Which of the following methods can be used for transactions in EF 6?

Starting with EF6 the framework now provides: Database. BeginTransaction() : An easier method for a user to start and complete transactions themselves within an existing DbContext – allowing several operations to be combined within the same transaction and hence either all committed or all rolled back as one.


2 Answers

No it is not necessary to explicitly call Rollback. The tx variable will be disposed when the using block finishes, and the transaction will be rolled-back if Commit() has not been called.

I have tested this using SQL Server Activity Monitor, by observing the locks held on the database objects, as well as querying against the database to observe when the data is rolled back, using the nolock hint in my select statement to be able to view uncommitted changes in the database.
E.g. select top 10 * from [tablename] (nolock) order by modifiedDate

like image 83
Teevus Avatar answered Sep 21 '22 17:09

Teevus


To the EF, the database provider is arbitrary and pluggable and the provider can be replaced with MySQL or any other database that has an EF provider implementation. Therefore, from the EF point of view, there is no guarantee that the provider will automatically rollback the disposed transaction, because the EF does not know about the implementation of the database provider.

This answer pretty much explains everything and confusion with all msdn docs having that Rollback called explicitly: https://stackoverflow.com/a/28915506/5867244

like image 22
Grigoryants Artem Avatar answered Sep 22 '22 17:09

Grigoryants Artem