修改后的数据在更新后调用时显示旧值

时间:2013-08-07 19:25:43

标签: c# entity-framework

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

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

该项目显示在构建之前它所持有的最后一个值,而不是当前值。在这种情况下,值将是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();
        }
    }
}

1 个答案:

答案 0 :(得分: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();
}

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