Is using TransactionScope in the Entity Framework a good idea?

I read a document from a group of Microsoft templates and practices ( Data Access for Highly Scalable Solutions: Using SQL Stability, NoSQL, and Polyglot Persistence ).

In Chapter 3, in the section "Retrieving Data from a SQL Server Database," the authors discuss using Entity Framework to load objects from a database. Here is a bit of their sample code:

using (var context = new PersonContext()) { Person person = null; using (var transactionScope = this.GetTransactionScope()) { person = context.Persons .Include(p => p.Addresses) .Include(p => p.CreditCards) .Include(p => p.EmailAddresses) .Include(p => p.Password) .SingleOrDefault(p => p.BusinessEntityId == personId); transactionScope.Complete(); } // etc... } 

Note the use of the custom transaction GetTransactionScope using the GetTransactionScope method, implemented in their base context like this:

 public abstract class BaseRepository { private static TransactionOptions transactionOptions = new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted }; protected virtual TransactionScope GetTransactionScope() { return new TransactionScope(TransactionScopeOption.Required, transactionOptions); } } 

Working with transactions (EF6 Onwards) in MSDN states:

In all versions of the Entity Framework, whenever you execute SaveChanges () to insert, update or delete in the database, the infrastructure will wrap this operation in a transaction [...], the transaction isolation level is any isolation level, the database provider considers the default setting. By default, for example, on SQL Server it is READ COMMITTED. Entity Framework does not transfer requests to transactions. . This functionality is by default suitable for a large number of users.

My bold accent.

My question is: is TransactionScope used , as shown above, overkill, especially for reading data, when using the Entity Framework?

(after I realized that the answer to this question may be somewhat based on opinions, sorry for that.)

+7
c # design-patterns entity-framework
source share
4 answers

The question is a little open. But it can be useful for people who learn about dirty readings. Isolation and EF. And you read the EF Transaction Scope ef6 , and we have a clear question.

All in all, I would say let EF manage the area. Beginners do not consider uncommitted readings and are a good standard for EF.

You may have a good reason to manage the area. Or you need to use an existing transaction. But it remains for the specialist.

So now the real question.
Is it useful to include such protective programming around isolation.

My opinion:
Only if that doesn't make the code harder to maintain and harder to reuse.

Oracle and SQL Server have Read Committed by default. I would expect this on other databases too. Therefore, I will conclude, most likely, unnecessary protection, which will add complexity.
I would not add it to my code.

+5
source share

It depends. If you want dirty reads, serializable reads, etc. Different from the default isolation level, then you need to wrap your queries inside the transaction scope.

+2
source share

To maintain data integrity, it is sometimes useful to use an explicitly defined transaction with several statements, so if any part of the transaction fails, it will roll back. The decision about whether to use a transaction should not be easy. Explicit transactions, especially distributed ones, bring significant performance benefits and open the system before deadlocks and the possibility that a transaction can be left open, lock the affected table and block all other transactions. In addition, code is much more difficult to develop and maintain.

In my experience, I have found that using a transaction scope in C # code is more complicated than using an explicit transaction in SQL. Therefore, for operations that benefit from a transaction and indeed any sufficiently complex query, I would recommend creating a stored procedure to be called from ORM.

+1
source share

Have you read the following link? https://msdn.microsoft.com/en-us/library/vstudio/bb738523%28v=vs.100%29.aspx

If you used the usual saveChanges behavior without starting the transaction part, you would not be able to extend the transaction outside the database. Using the transaction scope, as in the link above, makes the message queue part of the transaction. Therefore, if for any reason the sending of the message fails, the transaction also fails.

Of course, you can consider this a more complex scenario. In simple applications that do not require such complex logic, you are probably safer to use the simple saveChanges api.

+1
source share

All Articles