Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”?

What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”? I can not find it anywhere. Shall it be "Serializable"?

like image 962
BigMountainTiger Avatar asked Apr 14 '11 00:04

BigMountainTiger


People also ask

What is the default isolation level in Entity Framework?

Accepted Answer. By default, a Transaction has an IsolationLevel of Serializable. Serializable is the highest level. It requires that the transaction completes before any other transaction is allowed to operate on the data.

Which is the default transaction isolation level?

The isolation level of the transactional support is default to READ UNCOMMITTED. You can change it to READ COMMITTED SNAPSHOT ISOLATION by turning ON the READ_COMMITTED_SNAPSHOT database option for a user database when connected to the master database.

Is EF core SaveChanges transaction?

This feature was introduced in EF Core 5.0. When SaveChanges is invoked and a transaction is already in progress on the context, EF automatically creates a savepoint before saving any data. Savepoints are points within a database transaction which may later be rolled back to, if an error occurs or for any other reason.

Is SaveChanges a transaction?

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.


1 Answers

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

like image 90
Ladislav Mrnka Avatar answered Oct 02 '22 14:10

Ladislav Mrnka