当我发出SaveChanges()"时,Entity Framework 中的默认事务隔离级别是什么?我在任何地方都找不到它.应该是可序列化"吗?
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
使用当前存储提供程序的 DbTransaction
实现.这意味着默认事务隔离级别设置为数据库服务器的默认值.在 SQL Server 中,它是 READ COMMITTED
.如果要更改隔离级别,可以使用 TransactionScope
.您还可以在派生上下文中覆盖 SaveChanges
并在覆盖方法中直接将 base.SaveChanges()
包装到范围.
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();
}
}
您可以进一步改进此代码以允许您将隔离级别传递给 SaveChanges
等.一旦您开始更改隔离级别,您应该始终如一地执行此操作.这意味着每次要运行事务时都应该定义隔离级别,因为 隔离级别是按连接配置的 和连接在使用连接池时被重用.
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.
EF6 中的默认事务级别已更改为 READ COMMITTED SNAPSHOT
Default transaction level in EF6 has changed to READ COMMITTED SNAPSHOT
这篇关于当我发出“SaveChanges()"时,Entity Framework 中的默认事务隔离级别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!