0

The question asked on following link has a great answer under that as well but my problem starts after that.

Using Transactions or SaveChanges(false) and AcceptAllChanges()?

I tried the code given as per my requirement as follows


MyEntities context1 = new MyEntities();

...
...
 // some code for changing the table data in Context 1.
...

MyEntities context1 = new MyEntities();

using (TransactionScope scope = new TransactionScope())
{
    context1.SaveChanges(false);   // LABEL 1
    context2.SaveChanges(false);   // LABEL 2
    scope.Complete();             // LABEL 3
    context1.AcceptAllChanges();  // LABEL 4
    context2.AcceptAllChanges();  // LABEL 5
}

Things works fine till line marked with // LABEL 1 However on line // LABEL 2 it fails saying "The underlying provider failed on Open."

NOTE : - Please note that Context1, Context2 are 2 different instance of same Type.

Could anyone help me on this?

Community
  • 1
  • 1
Manish
  • 47
  • 5
  • 1
    The first step to debug anything is to look at the full error message. Look at the inner exception(s). – usr Jan 08 '13 at 20:47

1 Answers1

0

This answer might explain the cause of your problem: https://stackoverflow.com/a/3081776/655625 you might need to reopen the connection after the context1.SaveChanges(false); line. Such as with:

context2.Database.Connection.Open(); //before the second SaveChanges(false)

However, read carefully and you will find that you're doing a distributed transaction that way, this other answer (linked from the first one) shows an alternative (more elaborate) to avoid the distributed transaction. https://stackoverflow.com/a/794785/655625

Community
  • 1
  • 1
Chris Amelinckx
  • 4,334
  • 2
  • 23
  • 21