在我的应用程序中,我有一个方法可以像这样更新项目的值:
foreach (var orderDetailInfo in _order.mListOrderDetail)
{
OrderDetailInfo info = orderDetailInfo;
var inventoryQry = from i in db.INVENTORY
where i.INVENT_IDE == info.mInventoryID
select i;
INVENTORY originalItem = inventoryQry.Single();
INVENTORY itemToModify = originalItem;
itemToModify.INVENT_NB_IN_STOCK = (orderDetailInfo.mOrderDetailQuantity +
originalItem.INVENT_NB_IN_STOCK);
db.Entry(originalItem).CurrentValues.SetValues(itemToModify);
db.SaveChanges();
}
现在,如果我查看表中的originalItem
,则值已更新。假设在更新之前,它的值为 4,order
更新数量为 5。
我的问题是,当我在上面的代码运行之后加载这样的数据时(所以,在我更新INVENT_NB_IN_STOCK
列之后):
var inventoryQry = from inv in db.INVENTORY
where inv.INVENT_IDE == inventIDE
select inv;
return inventoryQry.Single();
该项目显示它在 building 之前持有的最后一个值,而不是当前值。在这种情况下,值将是 4,而不是 5。为什么?谁能帮我找出我的问题?
因为这可能是因为我做了一个巨大的方法,所以我在这里发布了包含此处显示的代码块的整个方法:
internal void ValidateSupplierOrder(OrderInfo _order, List<OrderDetailInfo> _listOldOrderDetails)
{
using (BlueBerry_MTGEntities db = new BlueBerry_MTGEntities())
{
using (TransactionScope scope = TransactionUtils.CreateTransactionScope())
{
try
{
ORDERS orderToModify = DataConverter.OrderInfoToOrderDATA(_order);
var orderQry = from o in db.ORDERS
where o.ORDER_IDE == _order.mOrderID
select o;
ORDERS originalOrder = orderQry.Single();
// First step: we need to remove the old order details as they are no more related
// To the order we are currently adding.
foreach (var orderDetail in _listOldOrderDetails)
{
OrderDetailInfo detail = orderDetail;
var orderDetailQry = from od in db.ORDER_DETAIL
where od.ORDER_DETAIL_IDE == detail.mOrderDetailID
select od;
ORDER_DETAIL itemToRemove = orderDetailQry.SingleOrDefault();
db.ORDER_DETAIL.Remove(itemToRemove);
db.SaveChanges();
}
foreach (ORDER_DETAIL orderDetailToCreate in _order.mListOrderDetail.Select(DataConverter.OrderDetailInfoToOrderDetailDATA))
{
db.ORDER_DETAIL.Add(orderDetailToCreate);
db.SaveChanges();
}
foreach (var orderDetailInfo in _order.mListOrderDetail)
{
OrderDetailInfo info = orderDetailInfo;
var inventoryQry = from i in db.INVENTORY
where i.INVENT_IDE == info.mInventoryID
select i;
INVENTORY originalItem = inventoryQry.Single();
INVENTORY itemToModify = originalItem;
itemToModify.INVENT_NB_IN_STOCK = (orderDetailInfo.mOrderDetailQuantity +
originalItem.INVENT_NB_IN_STOCK);
db.Entry(originalItem).CurrentValues.SetValues(itemToModify);
db.SaveChanges();
}
db.Entry(originalOrder).CurrentValues.SetValues(orderToModify);
db.SaveChanges();
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
scope.Complete();
}
}
}