Flair Flair - 6 months ago 82
C# Question

Database.BeginTransaction vs Transactions.TransactionScope

What is the difference between

and EF6's

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
and now MS has introduced this new
in EF6.


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(). System.Transactions.TransactionScope is just old style of writing transactional code.

But Database.BeginTransaction() is used only for database related operations transaction, whereas System.Transactions.TransactionScope makes the possible 'plain C# code' also 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.

Further information can be found here