我正在尝试使用 LINQ TO SQL 在现有事务中执行未提交的隔离级别的查询。如果我使用该选项从父事务中抑制此事务,似乎我失去了指定隔离级别的能力。在 LINQPad 中使用此代码:
void Main()
{
var db = this;
db.ExecuteQuery<string>(@"SET TRANSACTION ISOLATION LEVEL SERIALIZABLE");
using (var trans = new TransactionScope(TransactionScopeOption.Required))
{
// updates or inserts here
using (new TransactionScope(TransactionScopeOption.Suppress,
new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
{
db.ExecuteQuery<string>(@"SELECT CASE transaction_isolation_level
WHEN 0 THEN 'Unspecified'
WHEN 1 THEN 'ReadUncomitted'
WHEN 2 THEN 'Readcomitted'
WHEN 3 THEN 'Repeatable'
WHEN 4 THEN 'Serializable'
WHEN 5 THEN 'Snapshot' END AS TRANSACTION_ISOLATION_LEVEL
FROM sys.dm_exec_sessions
where session_id = @@SPID
").Dump("Inside the transaction");
}
// updates or inserts here
}
}
我得到了 SERIALIZABLE 的结果。有没有办法在事务中运行查询并将隔离级别更改为未提交的读取?