我正在评估EF(实体框架)是否适用于我的下一个新应用程序。
如何全局更改应用程序中所有EF事务的隔离级别? 例如:假设我想使用“读取提交的快照”。
虽然在需要TransactionScope时指定IsolationLevel是可以的(请参见下面的代码),但是将每个EF保存操作封装在TransactionScope中会很丑陋。
'OK
Using tsc As New TransactionScope(TransactionScopeOption.RequiresNew, TransactionOption.ReadCommitted)
UpdateShoppingCart
EnqueueNewOrder
SendConfirmationEmail
tsc.Complete
End Using
'Is this really the only way to avoid Serializable?
Using tsc As New TransactionScope(TransactionScopeOption.RequiresNew, TransactionOption.ReadCommitted)
_ctx.SaveChanges()
tsc.Complete
End Using
Class TransactionOption
Public Shared ReadOnly ReadCommitted As New TransactionOptions() With {
.IsolationLevel = IsolationLevel.ReadCommitted,
.Timeout = TransactionManager.DefaultTimeout
}
End Class
我认为混合使用隔离级别不是一个好主意。这种做法有什么问题吗?
使用Serializable和SQL Server(与Oracle相比)插入一个简单的读取可能会导致转换锁定死锁。
根据EF FAQ: "建议使用READ COMMITTED事务,并在需要读者不阻止写者且写者不阻止读者时使用READ COMMITTED SNAPSHOT ISOLATION."
我不理解为什么EF默认使用Serializable,并且使更改默认隔离级别变得如此困难 - 与SQL Server(与Oracle的多版本控制相比)默认使用悲观并发模型有关。配置选项应该非常容易实现 - 或者我错过了什么重要信息?