2014-10-27 81 views
0

在這裏我添加一些數據到數據庫使用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你請人給我的幫助這一點。 感謝

回答

1

如果我正確地閱讀本那麼你只是n eed創建另一個上下文的新實例,取回需要更新的記錄,更新該記錄和SaveChanges。

相關問題