0

Here's my issue

I have two forms, formA and formB, formB works as a dialog of formA

within the scope of formA, defined with

EntityContext contextA = new EntityContext();

somewhere in formA, it invokes

new formB().ShowDialog();

After invoking ShowDialog(), it comes with

contextA.SaveChanges(); //<<<A>>>

Within formB class, defined with

EntityContext contextB = new EntityContext();

Somewhere it triggered

{
bool transactionSucceed = false;
using(Transaction transaction = new Transaction())
{
  contextB_DataOperations(); 
  contextB.SaveChanges();
  transaction.complete();
  transactionSucceed = true;
}
if(transactionSucceed)
  contextB.AcceptAllChanges(); // <<<B>>>
}

formB is disposable, when disposing, it invokes

contextB.Dispose();

Where the problem is, expected transaction operational data commitment at <<<B>>>, it is actually committed at <<<A>>>

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

1

Hi simple way to do that is

      using (TransactionScope tx = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    contextB_DataOperations(); 
    contextB.SaveChanges();
    contextB.AcceptAllChanges();
    TransactionScope .complete();

}

Hope this will help

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • The above scenario you have mentioned is only because of Transaction Nesting. – yo chauhan Jul 05 '12 at 09:29
  • It does not work on my case.. While I want the change to be made within the form B's context, it still, commits the data when returned to form A and triggered contextA.SaveChanges() – Stanley w Jul 10 '12 at 01:47
  • Maybe I misunderstood the scope of two different contexts,they were nested. But my believe is even within contextA, contextB is segregated from contextA. All data changes within contextB, including transactions buffering and committing, were all executed or rollback'd when scope.Complete. Please correct me if my understanding are wrong. – Stanley w Jul 10 '12 at 01:48
  • [rephrase] Maybe I misunderstood the scope of the two nested contexts. My believe is even within contextA, contextB is running independently away from contextA. All data changes within contextB, including transactions buffering and committing, were all executed or rollback'd when scope.Complete(). Please correct me if my understanding are wrong. – Stanley w Jul 10 '12 at 01:55
  • I think its not because of nesting of contexts but its just because of nesting of Transactions. If you want inner transaction to work independently then set the TransactionScopeOption as ReqiresNew for inner Transaction and if you want inner transaction to work in sync with outer transaction then set the TransactionScopeOption for inner transaction as Required (and i think this is the case in your Transactions as inner transaction is committing when outer completes). – yo chauhan Jul 10 '12 at 03:41