添加到一个上下文并更新另一个上下文
问题描述:
在这里我添加一些数据到数据库使用EF代码。当我添加使用一个上下文它工作。但我需要更新另一个表(另一个上下文)使用ProductId(productId avaialble在iOdr对象。添加到一个上下文并更新另一个上下文
这里是我的代码
public int AddorUpdateSalesOrder(ref InvoiceHD iOdr)
{
try
{
if (iOdr.InvoiceId != null)
{
//context.Entry(iOdr).State = EntityState.Modified;
context.InvoiceHDs.Add(iOdr);
//Here I need to update Product context
//using ProductId(it's available in iOdr).
//If ProductId is 1 then I need to
//update product tables ProductQuantity coloumn with new value
}
else
{
context.InvoiceHDs.Add(iOdr);
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
return context.SaveChanges();
}
我不知道该怎么做第二context.could你请人给我的帮助这一点。 感谢
答
如果我正确地阅读本那么你只是n eed创建另一个上下文的新实例,取回需要更新的记录,更新该记录和SaveChanges。