0

在我的应用程序中,我有一个方法可以像这样更新项目的值:

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

1 回答 1

0

好吧,我发现了这个问题,这是由于……我自己缺少知识!

让我们看看这些代码行:

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

我的猜测是(并且是)不知何故我用来修改值的模式不是好的模式。也许是因为我在复制一个对象而不是创建一个新对象并将实际值设置为我需要的值。

所以我这样做了:

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

    int newStock = (int)(info.mOrderDetailQuantity + originalItem.INVENT_NB_IN_STOCK);

    INVENTORY itemToModify = new INVENTORY
        {
            CARD_IDE = originalItem.CARD_IDE,
            INVENT_IDE = originalItem.INVENT_IDE,
            INVENT_ITEM_STATE = originalItem.INVENT_ITEM_STATE,
            INVENT_NB_IN_STOCK = newStock,
            INVENT_NB_QT_ADJUST = 0,
            INVENT_NB_QT_EBAY_AUCTION = originalItem.INVENT_NB_QT_EBAY_AUCTION,
            INVENT_NB_QT_EBAY_STORE = originalItem.INVENT_NB_QT_EBAY_STORE,
            INVENT_NB_QT_FINEP = originalItem.INVENT_NB_QT_FINEP,
            INVENT_NB_QT_SEND = 0,
            INVENT_NB_QT_SOLD = originalItem.INVENT_NB_QT_SOLD
        };

    db.Entry(originalItem).CurrentValues.SetValues(itemToModify);

    db.Entry(originalItem).State = EntityState.Modified;

    db.SaveChanges();
}

虽然它看起来并不优雅,但它确实有效!

于 2013-08-08T13:52:49.953 回答