0

我正在测试 EF6 隔离级别,但测试失败:

Assert.AreEqual failed. Expected:<ReadUncommitted>. Actual:<Unspecified>.

考试:

public void TestIsolationLevelReadUncommitted()
{
  // Arrange
  using (
    new TransactionScope(TransactionScopeOption.Required,
      new TransactionOptions {IsolationLevel = IsolationLevel.ReadUncommitted}))
  {
    using (var context = new BoligEntities())
    {
      // Act
      context.GetDbConnection().Open();
      var isolationLevel = context.GetDbConnection().GetIsolationLevel();

      // Assert
      Assert.AreEqual(System.Data.IsolationLevel.ReadUncommitted, isolationLevel);
    }
  }
}

测试没有多大意义,但我想知道为什么它会失败。

4

2 回答 2

1

有很多关于事务范围和 EF 的帖子。
实际上将未提交的读取和 nolock 添加到您的搜索中。

良好的基本解释和示例

FROM EF 6 on... EF 事务范围文档

一般来说你不需要它。(有例外),我希望我不必支持使用未提交读取的系统。;-) 肮脏的...

祝你好运

于 2013-11-13T14:00:36.350 回答
1

事务与数据库不同。看起来您正在检查打开连接的隔离级别,而不是正在运行的事务。一般来说 - 您可以打开连接并在此连接上以不同的隔离级别运行多个事务。

using (var context = new MyEntities())
{
     using (var tran = context.Database.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted))
     {

         Assert.AreEqual(System.Data.IsolationLevel.ReadUncommitted, tran.UnderlyingTransaction.IsolationLevel);
于 2014-03-20T00:58:25.037 回答