2012-04-11 60 views
0

所有項目我有這樣的方法:的UnitOfWork不更新基於條件

public bool UpdateOfficeApprovers(IList<int> invoiceLinesToUpdate, int userId) 
{ 
    foreach (var invoiceLineId in invoiceLinesToUpdate) 
    { 
     var invoiceLine = _unitOfWork.InvoiceLineRepository.Get(invoiceLineId); 

     invoiceLine.OfficeUserId = userId; 

     if (!invoiceLine.HasTwoUniqueApprovers) 
     { 
      // do something here to avoid this line being updated 
     } 
    } 

    _unitOfWork.Save(); 

    return hasUniqueApprovers; 
} 

我想在這裏做的是通過所有的invoiceLines並更新其OfficeUserId。然而,有狀態HasTwoUniqueApprovers,如果這是false我不想更新此invoiceLine只是離開它。

好太行:

invoiceLine.OfficeUserId = userId; 

將更新實體狀態EntityState.Modified是否正確?

所以當:

_unitOfWork.Save(); 

這將保存所有的invoiceLInes因爲它節省了一切:

EntityState.Modified 

那麼我現在想知道的是如何被更新阻止某些invoiceLInes 。

所以當invoiceLine符合條件如何設置它,所以它不會被更新?

+0

檢查範圍和縮進,你有一個錯誤。我編輯了某種方式。 – abatishchev 2012-04-11 11:08:40

回答

0

未設置OfficeUserId你不想保存或設置其狀態恢復到未更改的行。

objectContext.ObjectStateManager.ChangeObjectState(invoiceLine, EntityState.Unchanged); 

或API的DbContext:

dbContext.Entry(invoiceLine).State = EntityState.Unchanged; 
2

檢查HasTwoUniqueApprovers的Innstead;只需檢查實體HasTwoUniqueApprovers是否更新此實體。 「HasTwoUniqueApprovers」爲false的其他實體將處於未更改狀態,並且不會在objectcontext中進行處理。

public bool UpdateOfficeApprovers(IList<int> invoiceLinesToUpdate, int userId) 

{ 
    foreach (var invoiceLineId in invoiceLinesToUpdate) 
    { 
     var invoiceLine = _unitOfWork.InvoiceLineRepository.Get(invoiceLineId); 


    if (invoiceLine.HasTwoUniqueApprovers) 
    { 
     invoiceLine.OfficeUserId = userId; 
    } 
} 

_unitOfWork.Save(); 

return hasUniqueApprovers; 
}