Wednesday, March 7, 2012

the underlying provider failed to open

You may receive this error if you open multiple connections within same transaction scope using Entity framework. You can open connection at top to solve this. if it is still not working, then you may have issue with MS-DTC (Distributed transaction co-ordinator).



   1:   var somedb = new SomeDbEntities();
   2:   
   3:  public void SomeMethodTOUpdateDb(){
   4:    using (TransactionScope scope = new TransactionScope())
   5:                  {
   6:                      //Generate link name
   7:                      somedb.Connection.Open();
   8:                      //do db related work
   9:                      Biller biller = new Biller();
  10:                      biller.BillerId = Guid.NewGuid();
  11:                      billerview.ToBiller(biller);
  12:                      billerview.BillerId = biller.BillerId; //update billerid
  13:                      db.Billers.AddObject(biller);
  14:                      YouCanCallAnotherMethodDoMoreDbWork(); 
  15:                      db.SaveChanges();
  16:                      //commit all
  17:                      scope.Complete();
  18:                   }
  19:  }

No comments:

Post a Comment

Hey!
Let me know what you think?