lig*_*ren 2 .net sql transactions read-uncommitted linq-to-sql
我们可以像这样使用SQL:
SELECT * FROM student WITH(NOLOCK);
Run Code Online (Sandbox Code Playgroud)
如何在不使用LINQ to SQL的情况下实现这一点TransactionScope
?
LINQ to SQL没有任何执行此操作的机制,但您可以创建具有特定隔离级别的事务.看下面的代码:
using (var con = new SqlConnection("constr"))
{
con.Open();
using (var transaction = con.BeginTransaction(
IsolationLevel.ReadUncommitted))
{
using (var context = new SchoolDataContext(con))
{
// HACK: Setting the context.Transaction is
// needed in .NET 3.5 (fixed in .NET 4.0).
context.Transaction = transaction;
var q = from s in context.Students select c;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有时使用这种类型的隔离是有用的,即出于性能原因.但请确保您不使用此类型的数据库隔离执行任何创建,更新或删除(CUD)操作.它当然取决于您的情况,但您的数据可能会处于不一致状态.