我已经使用 SQL 2008 R2 和 Entity Framework 4 完成了以下测试。(数据库的 READ_COMMITTED_SNAPSHOT 选项为 ON)
我创建了以下存储过程来返回上下文隔离级别(来自这里的原始文件):
Create Procedure TempTestIsolation
AS
Begin
DECLARE @UserOptions TABLE(SetOption varchar(100), Value varchar(100))
INSERT @UserOptions
EXEC('DBCC USEROPTIONS WITH NO_INFOMSGS')
SELECT Value
FROM @UserOptions
WHERE SetOption = 'isolation level'
End
然后我编写了以下测试:
static void Main(string[] args)
{
var entities = new MyEntities();
// Execute the SP to get the isolation level
string level = entities.TempTestIsolation().First().Value;
Console.WriteLine("Without a transaction: " + level);
var to = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.Snapshot };
using (var ts = new TransactionScope(TransactionScopeOption.Required, to))
{
// Execute the SP to get the isolation level
level = entities.TempTestIsolation().First().Value;
Console.WriteLine("With IsolationLevel.Snapshot: " + level);
}
to = new TransactionOptions() { IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted };
using (var ts = new TransactionScope(TransactionScopeOption.Required, to))
{
// Execute the SP to get the isolation level
level = entities.TempTestIsolation().First().Value;
Console.WriteLine("With IsolationLevel.ReadCommitted: " + level);
}
Console.ReadKey();
}
谁的输出是:
如您所见,当您在TransactionOptions中将IsolationLevel设置为Snapshot时,存储过程在“Snapshot”隔离级别下执行,而不是在“Read Committed Snapshot”下执行。
相反,如果您将IsolationLevel设置为ReadCommitted,它将在“Read Committed Snapshot”下执行。
希望能帮助到你。