本文介绍了实体框架与NOLOCK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Entity Framework上使用NOLOCK功能? XML是唯一的方法吗?
How can I use the NOLOCK function on Entity Framework? Is XML the only way to do this?
推荐答案
不,但您可以启动事务并设置。这与NOLOCK基本上是一样的,但不是按照每个表的方式进行,而是在交易范围内的一切。
No, but you can start a transaction and set the isolation level to read uncommited. This essentially does the same as NOLOCK, but instead of doing it on a per table basis, it will do it for everything within the scope of the transaction.
像你想要的,这里是你如何去做...
If that sounds like what you want, here's how you could go about doing it...
//declare the transaction options
var transactionOptions = new System.Transactions.TransactionOptions();
//set it to read uncommited
transactionOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
//create the transaction scope, passing our options in
using (var transactionScope = new System.Transactions.TransactionScope(
System.Transactions.TransactionScopeOption.Required,
transactionOptions)
)
//declare our context
using (var context = new MyEntityConnection())
{
//any reads we do here will also read uncomitted data
//...
//...
//don't forget to complete the transaction scope
transactionScope.Complete();
}
这篇关于实体框架与NOLOCK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!