这是我的问题
我有两个表单,表单A和表单B,表单B用作表单A的对话框
在形式 A 的范围内,定义为
EntityContext contextA = new EntityContext();
在窗体 A 中的某个地方,它调用
new formB().ShowDialog();
调用 ShowDialog() 后,它附带
contextA.SaveChanges(); //<<<A>>>
在 formB 类中,定义为
EntityContext contextB = new EntityContext();
它触发的某个地方
{
bool transactionSucceed = false;
using(Transaction transaction = new Transaction())
{
contextB_DataOperations();
contextB.SaveChanges();
transaction.complete();
transactionSucceed = true;
}
if(transactionSucceed)
contextB.AcceptAllChanges(); // <<<B>>>
}
formB 是一次性的,在处置时,它会调用
contextB.Dispose();
问题所在,预期事务操作数据承诺在<<<B>>>
,实际上是在<<<A>>>
嗨,
简单的方法是
using (TransactionScope tx = new TransactionScope(TransactionScopeOption.RequiresNew))
{
contextB_DataOperations();
contextB.SaveChanges();
contextB.AcceptAllChanges();
TransactionScope .complete();
}
希望这会有所帮助