Database.BeginTransaction vs Transactions.TransactionScope

36,752

Solution 1

I found out the answer in Entity Framework 6's documentation:

With the introduction of EF6, Microsoft recommends to use new API methods: Database.BeginTransaction() and Database.UseTransaction(). Although System.Transactions.TransactionScope is still very well supported, it is no longer necessary for most users of EF6.

While Database.BeginTransaction() is used only for database related operations transaction, System.Transactions.TransactionScope, in addition to that, makes it possible for 'plain C# code' to also be transactional.

Hence, use Database.BeginTransaction() where ever doing only db related operations in a transaction in EF6 otherwise use System.Transactions.TransactionScope for mixing db operations and C# code together in a transaction.

For those who still prefer the TransactionScope approach, it is recommended they checkout its limitations, especially in cloud scenarios (cloud scenarios do not support distributed transactions).

Further information can be found here

Solution 2

The accepted and popular answer is misleading. Both Database.BeginTransaction() and System.Transactions.TransactionScope are for DB operations.

The main differences between Database.BeginTransaction() and System.Transactions.TransactionScope:

Style

  • With TransactionScope you set the transactions implicitly in the background (by wrapping all transactional actions with a starting using scope = new TransactionScope and an ending scope.Complete(); to commit.
  • With Database.BeginTransaction the transaction is set explicitly by writing sqlCommand.Transaction = sqlTxn; and context.Database.UseTransaction(sqlTxn);

Distributed Transactions

  • TransactionScope supports both distributed transactions (where multiple DB involved in a single transaction) and non-distributed transactions.
  • Database.BeginTransaction only supports non-distributed transactions (a local transaction where all actions are done within a single DB).

MSDN do state that with the new Database.BeginTransaction() and Database.UseTransaction() APIs, the TransactionScope approach is no longer necessary for most users.

Advantages and disadvantages of TransactionScope:

Disadvantages of TransactionScope:

  • Requires .NET 4.5.1 or greater to work with asynchronous methods.
  • It cannot be used in cloud scenarios unless you are sure you have one and only one connection (cloud scenarios do not support distributed transactions).
  • It cannot be combined with the Database.UseTransaction() approach of the previous sections.
  • It will throw exceptions if you issue any DDL and have not enabled distributed transactions through the MSDTC Service.

Advantages of TransactionScope:

  • It will automatically upgrade a local transaction to a distributed transaction if you make more than one connection to a given database or combine a connection to one database with a connection to a different database within the same transaction (note: you must have the MSDTC service configured to allow distributed transactions for this to work).
  • Ease of coding. If you prefer the transaction to be ambient and dealt with implicitly in the background rather than explicitly under you control then the TransactionScope approach may suit you better.

Based on this MSDN article.

Share:
36,752

Related videos on Youtube

Flair
Author by

Flair

Updated on January 18, 2021

Comments

  • Flair
    Flair over 3 years

    What is the difference between System.Transactions.TransactionScope and EF6's Database.BeginTransaction?

    Could someone give a small example or just explain which one to use when with a clear difference?

    P.S: In my project, I'm using EF6. I've already read the documentation but it didn't help much. Also looked up the examples but they are rather using SqlConnection.BeginTransaction and now MS has introduced this new Database.BeginTransaction in EF6.

  • Triynko
    Triynko almost 6 years
    Unfortunately, you can't nest transactions with Database.BeginTransaction, whereas you can with TransactionScope.
  • Gerard
    Gerard over 4 years
    What does ''plain C# code' to also be transactional' really mean?
  • Rudey
    Rudey over 3 years
    @Gerard I assume it means that if you do SQL operations outside of EF (like using ADO.NET), those operations will also happen under the same transaction.
  • BornToCode
    BornToCode over 3 years
    @Rudey - This is not correct, SQL operations outside of EF are also supported with Database.BeginTransaction(). See my answer for the actual difference between Database.BeginTransaction and TransactionScope.
  • Shaun Wilson
    Shaun Wilson about 3 years
    there are so many layers of misinformation here it should not be the accepted answer.