I have created two different Contexts for two different databases in Entity Framework. Now I am trying to update these databases in single transaction. My code is like that:
public class LPO_BLL
{
internal Context1 _context1 = null;
internal Context2 _Context2 = null;
public LPO_Detail_BLL(Context1 context1, Context2 context2)
{
_context1 = context1;
_context2 = context2;
}
public void Insert(PM_LPO lpo, LPO_Transaction lpo_transaction)
{
using (TransactionScope transaction = new TransactionScope())
{
_context1.LPO.Add(lpo);
_context1.SaveChanges();
_context2.LPO_Transaction.Add(lpo_transaction);
_context2.SaveChanges(); // I am getting error here...
transaction.Complete();
}
}
}
And in UI project, I am calling this as:
LPO lpo = new LPO();
//setting properties of lpo
LPO_Transaction lpo_trans = new LPO_Transaction();
//setting properties of lpo_trans
Context1 _context1 = new Context1();
//Opening _context1 connection and etc
Context2 _context2 = new Context2();
//Opening _context2 connection and etc
LPO_BLL lpo_bll = new LPO_BLL(_context1, _context2);
lpo_bll.Insert(lpo,lpo_trans);
At the moment, I am getting error : The underlying provider failed on EnlistTransaction
After searching over the internet for last 3 hours and trying different hit and trial methods, I decided to put this on SO. So far, i have found these two links to little bit closer:
TransactionScope - The underlying provider failed on EnlistTransaction. MSDTC being aborted