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

佐手、 提交于 2020-01-09 06:51:10

问题


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


回答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




回答2:


By default, the System.Transactions infrastructure creates Serializable transactions.




回答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.



来源:https://stackoverflow.com/questions/5657145/what-is-the-default-transaction-isolation-level-in-entity-framework-when-i-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!