【发布时间】:2013-12-09 10:27:24
【问题描述】:
我在实体框架中为两个不同的数据库创建了两个不同的上下文。现在我正在尝试在单个事务中更新这些数据库。我的代码是这样的:
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();
}
}
}
在 UI 项目中,我将其称为:
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);
目前,我收到错误: 基础提供商在 EnlistTransaction 上失败
在互联网上搜索了最后 3 个小时并尝试了不同的点击和试用方法后,我决定将它放在 SO 上。到目前为止,我发现这两个链接更接近一点:
TransactionScope - The underlying provider failed on EnlistTransaction. MSDTC being aborted
【问题讨论】:
标签: c# asp.net .net entity-framework transactions