0

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?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Bryce Martin
  • 129
  • 3
  • 17

2 Answers2

2

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.

2

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.
                }
Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
  • This worked like a charm. now I need to learn why the way I was doing it didn't work... I though that since I was doing a Select on that property and then updating it, then that would work as well... keep in mind that idea that I didn't originally do anything with newinv, just updated inventory directly. – Bryce Martin Apr 09 '13 at 18:31
  • The reason why it didn't update with your original code is the Select, you're selecting the variable, rather than the actual database object. You need to change the object explicitly. – Chris Dixon Apr 10 '13 at 08:00