我试图在内存中创建事务,并在tutorial之后创建了一个示例资源管理器:
public class VolatileRM : IEnlistmentNotification
{
private int memberValue = 0;
private int oldMemberValue = 0;
public int MemberValue
{
get { return memberValue; }
}
public void SetMemberValue(int newMemberValue)
{
Transaction currentTx = Transaction.Current;
if (currentTx != null)
{
Console.WriteLine("VolatileRM: SetMemberValue -
EnlistVolatile");
currentTx.EnlistVolatile(this, EnlistmentOptions.None);
}
oldMemberValue = memberValue;
memberValue = newMemberValue;
}
public void Commit(Enlistment enlistment)
{
Console.WriteLine("VolatileRM: Commit");
oldMemberValue = 0;
}
public void InDoubt(Enlistment enlistment)
{
}
public void Prepare(PreparingEnlistment preparingEnlistment)
{
preparingEnlistment.Prepared();
}
public void Rollback(Enlistment enlistment)
{
Console.WriteLine("VolatileRM: Rollback");
// Restore previous state
memberValue = oldMemberValue;
oldMemberValue = 0;
}
}
我可以通过执行以下操作来创建交易范围,
using (TransactionScope txSc = new TransactionScope())
{
vrm = new VolatileRM();
vrm.SetMemberValue(3);
txSc.Complete();
}
但是,我希望事务范围仅适用于我的InMemory事务,而不适用于其中的其他任何对象,例如EntityFramework或sql。
如何将TransactionScope
的范围限制为仅应用于自定义对象?