C# UnitOfWork没有';t根据条件更新所有项目
我有这个方法:C# UnitOfWork没有';t根据条件更新所有项目,c#,.net,entity-framework,unit-of-work,C#,.net,Entity Framework,Unit Of Work,我有这个方法: public bool UpdateOfficeApprovers(IList<int> invoiceLinesToUpdate, int userId) { foreach (var invoiceLineId in invoiceLinesToUpdate) { var invoiceLine = _unitOfWork.InvoiceLineRepository.Get(invoiceLineId); i
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;
}
将实体状态更新为EntityState。修改后的正确吗
因此,当:
_unitOfWork.Save();
这将保存所有发票行,因为它使用以下各项保存所有内容:
EntityState.Modified
所以我想知道的是如何阻止某些发票行被更新
因此,当发票行满足条件时,我如何设置它以使其不被更新?或者不为您不想保存的行设置OfficeUserId
,或者将其状态设置回未更改状态
objectContext.ObjectStateManager.ChangeObjectState(invoiceLine, EntityState.Unchanged);
或在DbContext API中:
dbContext.Entry(invoiceLine).State = EntityState.Unchanged;
而不是检查!有两个唯一的批准人;只需检查实体是否有两个唯一的审批人,然后更新此实体。“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;
}
public bool UpdateOfficeApprovers(IList invoiceLinesToUpdate,int userId)
{
foreach(invoiceLinesToUpdate中的var invoiceLineId)
{
var invoiceLine=_unitOfWork.InvoiceLineRepository.Get(invoiceLineId);
if(invoiceLine.HasTwoUniqueApprovers)
{
invoiceLine.OfficeUserId=userId;
}
}
_unitOfWork.Save();
返回唯一批准人;
}
检查范围和缩进,您有一个错误。我不知怎么编辑过。
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;
}