Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope transaction = new TransactionScope() VS TransactionScope s = context.Connection.BeginTransaction()

I just want to know if I want to rollback all changes in database if transaction not complete, is there any difference between

 using (TransactionScope transaction = new TransactionScope())

and

using (var dbContextTransaction = context.Database.BeginTransaction())

I am confused when read these two:

Connection.BeginTransaction and Entity Framework 4?

and

https://learn.microsoft.com/en-us/ef/ef6/saving/transactions

**note that I use entity framework 4 in my project if its necessary

like image 349
Sepehr Estaki Avatar asked Oct 27 '25 05:10

Sepehr Estaki


1 Answers

From Programming Microsoft SQL Server 2012:

There are a number of pain points with explicit transactions. The first difficulty lies in the requirement that every SqlCommand object used to perform updates inside the transaction must have its Transaction property set to the SqlTransaction object returned by BeginTransaction. This means that you must take care to pass along the SqlTransaction object to any place in your code that performs an update, because failing to assign it to the Transaction property of every SqlCommand object in the transaction results in a runtime exception. The issue is compounded when you need to track the SqlTransaction object across multiple method calls that perform updates. It becomes even harder to manage things when these methods need to be flexible enough to work whether or not a transaction is involved or required.

The problem is worse when working with any of the other technologies we'll be covering later that provide abstraction layers over the raw objects. For example, because a SqlDataAdapter actually wraps three distinct SqlCommand objects (for insert, update, and delete), you must dig beneath the data adapter and hook into its three underlying command objects so that you can set their Transaction properties. (We don't generally recommend that you mix and match different data access APIs within your application, but if you must transactionalize updates across a combination of technologies, implicit transactions make it easy.)

The TransactionScope object, introduced as part of a dedicated transaction management API with .NET 2.0 in 2005, lets you code transactions implicitly. This is a superior approach that relieves you from all of the aforementioned burdens associated with explicit transactions. So the guidance here is to always work with implicit transactions whenever possible. You will write less code that is more flexible when you allow the framework to handle transaction management for you. However, it is still also important to understand explicit transactions with the SqlTransaction object, as you might need to integrate with and extend existing code that already uses explicit transactions. Therefore, we will cover both transaction management styles to prepare you for all situations.

The transaction management API offers many more benefits besides implicit transactions. For example, TransactionScope is capable of promoting a lightweight transaction (one associated with a single database) to a distributed transaction automatically, if and when your updates involve changes across multiple databases.

like image 164
JohnyL Avatar answered Oct 29 '25 21:10

JohnyL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!