为何EF&如果我尝试在父类中使用TransactionScope,NUnit会抛出异常吗?

时间:2014-08-21 15:45:14

标签: c# entity-framework nunit

如果我执行以下操作:

public class AIntegrationTests
{

    protected TransactionScope transaction = null;

    [SetUp]
    public void SetUp()
    {
        transaction = new TransactionScope(TransactionScopeOption.Required);
    }

    [TearDown]
    public void TearDown()
    {
        transaction.Dispose();
    }

}

[TestFixture]
public class UserRepositoryIntegrationTests : AIntegrationTests
{
    private UserRepository _sut;

    [SetUp]
    public void SetUp()
    {
        _sut = new UserRepository();

    }

    //tests
}

我收到此错误:

 network access for distributed transaction manager (msdtc) has been disabled 
Please enable Network DTC access because of qlBulkCopy.WriteToServer(DataTable table) 
for particular database

当我从父类中删除事务实例化并将其放回子类中时 我没有错。

public class AIntegrationTests
{

    protected TransactionScope transaction = null;

    [SetUp]
    public void SetUp()
    {

    }

    [TearDown]
    public void TearDown()
    {
        transaction.Dispose();
    }

}

[TestFixture]
public class UserRepositoryIntegrationTests : AIntegrationTests
{
    private UserRepository _sut;

    [SetUp]
    public void SetUp()
    {
        _sut = new UserRepository();
       transaction = new TransactionScope(TransactionScopeOption.Required);
    }

    //tests
}

为什么我不允许在父类中实例化事务。如果我离开Dispose电话 父母一切正常。

1 个答案:

答案 0 :(得分:1)

派生类中的

SetUp()SetUp()隐藏在基类中,因此永远不会调用AIntegrationTests的{​​{1}}。明确地从SetUp()的{​​{1}} ...

中调用它
UserRepositoryIntegrationTests

由于您可以控制这两个类,因此您应该在基类中创建SetUp() [TestFixture] public class UserRepositoryIntegrationTests : AIntegrationTests { private UserRepository _sut; [SetUp] public void SetUp() { _sut = new UserRepository(); base.SetUp(); } //tests } 并覆盖它...

SetUp()