Stanley w
Stanley w

Reputation: 1

Unexpected transaction commit, in nested entity context, entity framework 4.1

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>>>

Upvotes: 0

Views: 533

Answers (1)

yo chauhan
yo chauhan

Reputation: 12295

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

Upvotes: 1

Related Questions