【问题标题】:Store update, insert, or delete statement affected an unexpected number of rows (0)存储更新、插入或删除语句影响了意外的行数 (0)
【发布时间】:2015-03-10 00:21:03
【问题描述】:

我尝试更新我的 Products 表,但我不能因为抛出错误。

这是我的硬编码控制器:

[HttpPost]
public ActionResult EditProduct(ProductsViewModel productViewModel)
{
    TechStoreEntities context = new TechStoreEntities();

    Product newProduct = new Product
    {
        ProductId = productViewModel.ProductId,
        Name = productViewModel.Name,
        Price = productViewModel.Price,
        Discount = productViewModel.Discount,
        Quantity = productViewModel.Quantity,
        Size = productViewModel.Size,
        Description = productViewModel.Description,
        ProducerName = productViewModel.ProducerName,
        PaymentMethods = productViewModel.PaymentMethods,
        CategoryID = productViewModel.CategoryID,
        SubcategoryID = productViewModel.SubcategoryID,
        IsNew = productViewModel.IsNew,
        IsEnable = productViewModel.IsEnable
    };

    context.Entry(newProduct).State = EntityState.Modified;
    context.SaveChanges();

    ViewBag.CategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID == null), "CategoryID", "Name");
    ViewBag.SubcategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID != null), "CategoryID", "Name");

    return RedirectToAction("Products");
}

这是一个模型:

public class ProductsViewModel
{
    public int ProductId { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
    public int Discount { get; set; }
    public int Quantity { get; set; }
    public string Size { get; set; }
    public string Description { get; set; }
    public string ProducerName { get; set; }
    public string PaymentMethods { get; set; }
    public bool IsNew { get; set; }
    public bool IsEnable { get; set; }
    public string Category { get; set; }
    public int CategoryID { get; set; }
    public string Subcategory { get; set; }
    public int SubcategoryID { get; set; }
    public DateTime? CreateDate { get; set; }
}

我使用强类型视图:

@model MyTechStore.Models.ProductsViewModel

我在视图中添加:

@Html.HiddenFor(model => model.ProductId)

当我启动应用程序并输入一些数据以更新现有数据并按保存时,抛出异常:

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

当我调试时,我看到只有 ProductId 为 0。其他一切正常。我用脚手架控制器测试过,但没问题。我想使用视图模型,而不是像脚手架控制器那样使用我数据库中的模型。

谁能告诉我哪里错了?

我的 GET 方法:

public ActionResult EditProduct(int? id)
{
    TechStoreEntities context = new TechStoreEntities();

    ProductsManipulate product = new ProductsManipulate();

    ProductsViewModel editProduct = product.EditProduct(id);

    ViewBag.CategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID == null), "CategoryID", "Name");
    ViewBag.SubcategoryID = new SelectList(context.Categories.Where(c => c.SubCategoryID != null), "CategoryID", "Name");
    return View(editProduct);
}

还有我的数据访问层:

public ProductsViewModel EditProduct(int? id)
{
    TechStoreEntities context = new TechStoreEntities();
    Product dbProduct = context.Products.Find(id);
    ProductsViewModel product new ProductsViewModel
    {
        Name = dbProduct.Name,
        Price = dbProduct.Price,
        Quantity = dbProduct.Quantity,
        CategoryID = dbProduct.CategoryID,
        SubcategoryID = dbProduct.SubcategoryID,
        IsNew = dbProduct.IsNew
    };
    return product;
}

【问题讨论】:

  • 您能否在EditProduct 操作中显示您的[HttpGet]
  • 在编辑产品的时候,并不是要得到一个新产品,你要做的是在得到上下文之后,得到你要编辑的产品,比如 Product product = context.Products.Where(p => p.ProductId == productViewModel.ProductId) 然后像你现在做的那样将你的视图模型映射到这个现有的产品

标签: c# asp.net-mvc entity-framework


【解决方案1】:

您需要在ProductsViewModel 中填充ProductId

public ProductsViewModel EditProduct(int? id)
{
    TechStoreEntities context = new TechStoreEntities();

    Product dbProduct = context.Products.Find(id);

    ProductsViewModel product = new ProductsViewModel()
    {
        // You need this line to pass the value to View
        ProductId = dbProduct.ProductId,

        Name = dbProduct.Name,
        Price = dbProduct.Price,
        Quantity = dbProduct.Quantity,
        CategoryID = dbProduct.CategoryID,
        SubcategoryID = dbProduct.SubcategoryID,
        IsNew = dbProduct.IsNew
    };

    return product;
}

【讨论】:

  • 非常感谢。这就是问题所在:)
【解决方案2】:

该错误的意思是,EF 尝试使用这些字段更新 Product,但它返回 0 RowCount,因此它知道出了点问题。

正如您之前提到的,ProductId0,这意味着您可能没有具有该 ID 的Product,因此当 EF 尝试更新它时,行数为 0,这会导致EF 抛出一个DbUpdateConcurrencyException

如果您想更新现有产品,您需要确保填写您的 ID。

否则,如果您想要一个 upsert(更新或插入),您首先需要检查给定 ProductId 的记录是否存在,如果存在,请更新,否则执行插入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    相关资源
    最近更新 更多