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(); }
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.)
c # design-patterns entity-framework
Steven ands
source share