.net 当我发出“SaveChanges()”时,Entity Framework 中的默认事务隔离级别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5657145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”?
提问by BigMountainTiger
What is the default transaction isolation level in Entity Framework when I issue “SaveChanges()”? I can not find it anywhere. Shall it be "Serializable"?
当我发出“SaveChanges()”时,Entity Framework 中的默认事务隔离级别是什么?我在任何地方都找不到它。它应该是“可序列化的”吗?
回答by Ladislav Mrnka
SaveChangesuses implementation of DbTransactionfor 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 SaveChangesin your derived context and wrap base.SaveChanges()to the scope directly in overriden method.
SaveChanges使用DbTransaction当前存储提供程序的实现。这意味着默认事务隔离级别设置为数据库服务器的默认值。在 SQL Server 中,它是READ COMMITTED. 如果要更改隔离级别,可以使用TransactionScope. 您还可以SaveChanges在派生上下文base.SaveChanges()中覆盖并直接在覆盖方法中包装到范围。
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 SaveChangesetc. 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 connectionand connections are reused when using connection pooling.
您可以进一步改进此代码以允许您将隔离级别传递给SaveChanges等。一旦您开始更改隔离级别,您应该始终如一地执行此操作。这意味着每次要运行事务时都应该定义隔离级别,因为每个连接都配置了隔离级别,并且在使用连接池时会重用连接。
Edit:Default transaction level in EF6 has changed to READ COMMITTED SNAPSHOT
编辑:EF6 中的默认事务级别已更改为READ COMMITTED SNAPSHOT
回答by Kayode Leonard
By default, the System.Transactions infrastructure creates Serializable transactions.
默认情况下,System.Transactions 基础结构创建可序列化的事务。
回答by grahamesd
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)
从 EF 6 开始,SQL Server 事务的默认隔离级别是 READ COMMITTED。参考在这里: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.
对于其他提供者(相同的参考)“事务的隔离级别是数据库提供者认为其默认设置的任何隔离级别”。因此,您必须查看该提供商的文档。

