.BeginTransaction vs Transactions.TransactionScope Database

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

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

PS: In my project I use EF6. I already read the documentation, but it didn’t help much. They also looked at examples, but they rather use SqlConnection.BeginTransaction , and now MS has introduced this new Database.BeginTransaction in EF6.

+52
c # entity-framework transactions transactionscope entity-framework-6
Mar 13 '14 at 15:09
source share
1 answer

I found the answer in the Entity Framework 6 documentation:

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

While Database.BeginTransaction() used only for database transaction transactions, System.Transactions.TransactionScope , in addition to this, makes it possible for "simple C # code" to also be transactional.

Therefore, use Database.BeginTransaction() , where ever only operations related to db were performed in a transaction in EF6, otherwise use System.Transactions.TransactionScope to mix db operations and C # code together in a transaction.

For those who still prefer the TransactionScope approach, we recommend checking its limitations, especially in cloud scenarios (cloud scripts do not support distributed transactions).

Further information can be found here.

+65
Mar 14 '14 at 13:17
source share



All Articles