我有以下代码来更新一些库存值...
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();
}
}
SaveChanges()
似乎没有更新任何东西。我已经对其进行了调试,并且库存值已更改为所需的值,但由于某种原因它没有更新。我在循环内和循环外都试过了,都没有改变。有任何想法吗?我在这里想念什么?