What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”?
Solution 1
SaveChanges
uses implementation of DbTransaction
for current store provider. It means that default transaction isolation level is set to default value for the database server. In SQL Server it is READ COMMITTED
. If you want to change isolation level you can use TransactionScope
. You can also override SaveChanges
in your derived context and wrap base.SaveChanges()
to the scope directly in overriden method.
public override void SaveChanges()
{
// Default isolation level for TransactionScope is Serializable
using (var scope = new TransactionScope())
{
base.SaveChanges();
scope.Complete();
}
}
You can further improve this code to allow you passing isolation level to SaveChanges
etc. Once you start changing isolation levels you should do it consistently. It means you should define isolation level each time you want to run a transaction because isolation level is configured per connection and connections are reused when using connection pooling.
Edit: Default transaction level in EF6 has changed to READ COMMITTED SNAPSHOT
Solution 2
By default, the System.Transactions infrastructure creates Serializable transactions.
Solution 3
As of EF 6, the default isolation level for a SQL Server transaction is READ COMMITTED. The reference is here: Entity Framework Working with Transactions (EF6 Onwards)
For other providers (same reference) "the isolation level of the transaction is whatever isolation level the database provider considers its default setting". So you will have to look at the documentation of that provider.
BigMountainTiger
Updated on July 24, 2020Comments
-
BigMountainTiger almost 4 years
What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”? I can not find it anywhere. Shall it be "Serializable"?
-
Chad Moran about 13 yearsWouldn't this be useless since the code has to happen within the scope of the transaction itself?
-
dani herrera almost 11 yearsperhaps is needed to enclose base.savechanges into a try cacth, just to perform scope.complete if base.savechanges fail:
catch e { scope.complete(); throw }
. -
dani herrera almost 11 years@ChadMoran, can you explain your comment?
-
grahamesd over 10 yearsAdditional data on this answer here: Tips to avoid deadlocks in Entity Framework applications
-
Christian over 7 years@Ladislav Mrnka, Maybe I am wrong but "READ_COMMITTED_SNAPSHOT" shouldn't be an isolation level. It should be an option that can be set in order to modify the behavior of "READ_COMMITTED" isolation level. On the other side, how can "READ_COMMITTED_SNAPSHOT" be default used by EF if this option has to be explicitly enabled on the SQL Server? Isolation levels are documented here msdn.microsoft.com/en-us/library/ms173763.aspx, by the way at the same link we can find the explanation for READ_COMMITTED_SNAPSHOT option. Am I missing something?
-
grahamesd about 6 yearsAccording to this article, in EF6 (msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx) "the isolation level of the transaction is whatever isolation level the database provider considers its default setting"