0

我有这个方法:

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();

这将保存所有的 invoiceLINEes,因为它保存了所有内容:

EntityState.Modified

所以我想知道的是如何阻止某些invoiceLINEes 被更新。

那么当 invoiceLine 满足条件时,我该如何设置它以使其不会被更新?

4

2 回答 2

2

而不是检查 !HasTwoUniqueApprovers; 只需检查实体 HasTwoUniqueApprovers 是否然后更新此实体。“HasTwoUniqueApprovers”为假的其他实体将处于未更改状态,并且不会在对象上下文中进行处理。

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;
}
于 2012-04-11T11:13:35.527 回答
0

OfficeUserId为您不想保存的行设置或将其状态设置回未更改。

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

或在 DbContext API 中:

dbContext.Entry(invoiceLine).State = EntityState.Unchanged;
于 2012-04-11T11:09:10.390 回答