首页 > 解决方案 > 使用 ASP.NET MVC 和实体框架保持库存水平

问题描述

我正在尝试构建一个跟踪商店库存水平的应用程序。我遇到的问题是您可以同时从手头的数量中扣除多个“职员”。我知道并发是必需的,但我不确定如何处理它。为简单起见,我有以下代码:

public class Inventory{
     public int InventoryId {get;set;}
     public string InventoryName {get;set;}
     public double InventoryValue {get;set;}
     public virtual ICollection<Products> ProductsInInventory {get;set;}
}

public class Product{
     public int ProductId{get;set;}
     public string Name {get;set;}
     public double MinLevel {get;set;}
     public double ReorderAmount {get;set;}
     public double CurrentAmount {get;set;}
     [ForeignKey("InInventory")]
     public int InventoryId {get;set;}
     public virtual Inventory InInventory {get;set;}
     [ConcurrencyCheck]
     [Timestamp]
     public byte[] RowVersion { get; set; }
}
public class ProductOrder{
     public int ProductOrderId {get;set;}
     public virtual Product OrderProduct {get;set;}
     [ForeignKey("OrderProduct")]
     public int ProductId {get;set;}
     public virtual Order InOrder {get;set;}
     [ForeignKey("InOrder")]
     public int OrderId {get;set;}
     public double AmountProduct {get;set;}
}
public class Order{
     public int OrderId {get;set;}
     public DateTime DateOfOrder {get;set;}
     public virtual List<ProductOrder> ProductsOnOrder {get;set;}
     public double OrderAmount {get;set;}
}

对此的任何帮助将不胜感激。我知道我必须将此部分添加到代码中:

catch (DbUpdateConcurrencyException ex)
{
     var entry = ex.Entries.Single();
     var productValues = (Product)entry.Entity;
     var databaseEntry = entry.GetDatabaseValues();
     if (databaseEntry == null)
     {
          ModelState.AddModelError(string.Empty,"Unable to save changes. The product was deleted by another user.");
     }
     else{
         **This is where I assume I would deduct the product amount.  I am unsure of how to deduct from the new amount as it can be a high volume system.**
     }
}
catch (RetryLimitExceededException /* dex */)
{
//Log the error (uncomment dex variable name and add a line here to write a log.)
     ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
}

谢谢你们每一个人的帮助!!

标签: c#asp.net-mvcentity-frameworkconcurrency

解决方案


推荐阅读