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
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
SqlCommandobject used to perform updates inside the transaction must have its Transaction property set to theSqlTransactionobject returned byBeginTransaction. This means that you must take care to pass along theSqlTransactionobject to any place in your code that performs an update, because failing to assign it to theTransactionproperty of everySqlCommandobject in the transaction results in a runtime exception. The issue is compounded when you need to track theSqlTransactionobject 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
SqlDataAdapteractually wraps three distinctSqlCommandobjects (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 theirTransactionproperties. (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
TransactionScopeobject, 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 theSqlTransactionobject, 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,
TransactionScopeis 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With