为什么EF &;如果我试图在父类中使用TransactionScope,则抛出异常
本文关键字:TransactionScope 抛出异常 父类 EF 如果 为什么 | 更新日期: 2023-09-27 18:19:03
如果我这样做:
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
}
为什么不允许在父类中实例化事务?如果我留下处置电话
派生类中的SetUp()
隐藏了基类中的SetUp()
,因此AIntegrationTests
的SetUp()
永远不会被调用。从UserRepositoryIntegrationTests
的SetUp()
…中显式调用它
[TestFixture]
public class UserRepositoryIntegrationTests : AIntegrationTests
{
private UserRepository _sut;
[SetUp]
public void SetUp()
{
_sut = new UserRepository();
base.SetUp();
}
//tests
}
既然你可以控制这两个类,你应该让SetUp()
virtual
在基类中,并覆盖它…
public class AIntegrationTests
{
protected TransactionScope transaction = null;
[SetUp]
public virtual void SetUp()
{
transaction = new TransactionScope(TransactionScopeOption.Required);
}
[TearDown]
public void TearDown()
{
transaction.Dispose();
}
}
[TestFixture]
public class UserRepositoryIntegrationTests : AIntegrationTests
{
private UserRepository _sut;
[SetUp]
public override void SetUp()
{
_sut = new UserRepository();
base.SetUp();
}
//tests
}