Question

I have the following code to update some inventory values...

private static void UpdateInventory(int prodId, int qty)
    {
        using (var context = new uStore7_1Entities())
        {
            //Get the catalogNo and ProductUnitID of the product passed in so we can find all identical products that might just be boxed differently
            var currProdItem = context.Products.Where(c => c.ProductID.Equals(prodId))
                                       .Select(c => new {c.CatalogNo, c.ProductUnitID}).FirstOrDefault();

            //Get the units per box factor for calculating total product ordered
            var prodIdAmount =
                context.ProductUnits.Where(pa => pa.ProductUnitID.Equals(currProdItem.ProductUnitID))
                       .Select(pa => pa.Amount)
                       .FirstOrDefault();

            //Calculate the total number of units for this item
            var prodUnits = qty*prodIdAmount;

            //Get the entire list of products with the specified catalog number excluding the product passed in
            var uStoreProducts =
                context.Products.Where(p => p.CatalogNo.Equals(currProdItem.CatalogNo) && !p.ProductID.Equals(prodId))
                       .Select(p => p.ProductID);


            //Loop through each product in the uStoreProductsList
            foreach (var uStoreProduct in uStoreProducts)
            {
                var currentProduct = uStoreProduct;

                //Get the current product's ProductUnitId to get the 'pieces' per "box"
                var currentUnitId =
                    context.Products.Where(u => u.ProductID.Equals(currentProduct))
                           .Select(u => u.ProductUnitID)
                           .FirstOrDefault();

                //Use the ProductUnitId to get the "Amount" from the ProductUnits table.
                var inventoryFactor =
                    context.ProductUnits.Where(i => i.ProductUnitID.Equals(currentUnitId))
                           .Select(i => i.Amount)
                           .FirstOrDefault();

                //Divide the quantity passed 
                var qtyInUnits = prodUnits/inventoryFactor;

                var inventory =
                    context.ProductInventories.Where(pi => pi.ProductID.Equals(currentProduct))
                           .Select(pi => pi.InventoryQuantity)
                           .FirstOrDefault();

                /*var inventory = (from i in context.ProductInventories
                                where i.ProductID == currentProduct
                                select i).FirstOrDefault();
                */


                if (inventory != null)
                {
                    var newinv = inventory - qtyInUnits;
                    inventory = newinv;
                    //context.SaveChanges();
                }
            }
            context.SaveChanges();

        }

    }

The SaveChanges() does not seem to be updating anything. I have debugged it and the inventory value gets changed to the value needed, but for some reason it isn't updating. I've tried it inside the loop and outside the loop, both with no change. Any ideas? What am I missing here?

Was it helpful?

Solution

Your problem is object replacement, rather than re-assignment. When "newinv" is created, it's detached from your context, whereas "inventory" is the attached object. When "newinv" is assigned to "inventory", then it loses the association with the context.

Try this:

    var prodInventory =
                   context.ProductInventories
                   .Where(pi => pi.ProductID.Equals(currentProduct))
                   .FirstOrDefault();

                if (prodInventory != null)
                {
                    var newinv = prodInventory.InventoryQuantity - qtyInUnits;
                    prodInventory.InventoryQuantity = newinv; // This updates the actual context object now.
                }

OTHER TIPS

Your code does not update anything. All you do is gather entities and assign calculated values in local variables. But you never actually change a property of one of your entities.

Note that selecting properties of your entities, storing them in variables and replacing the value of this variable does not work. You need to select the entity if you want to modify it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top