存储更新、插入或删除语句影响了意外数量的行 (0),

Store update, insert, or delete statement affected an unexpected number of rows (0),

希望你一切顺利。

请帮助我解决 this.I 我在 ASP.NET mvc web 应用程序中使用以下代码。

[HttpPost]
        public ActionResult ProductAddonsAdd(int? id, tblproductattributemapper model, string AllStorechk, int? ddlProduct, int? ddlCompany, string hdCompanyProductID)
        {

            DbTransaction dbTran = null;
            try
            {
                tblproductattributemapper tblattributValMapper = new tblproductattributemapper();
                db.Connection.Open();
                dbTran = db.Connection.BeginTransaction();
                int CompanyProductID = 0;
                if(!string.IsNullOrEmpty(hdCompanyProductID))
                {
                    CompanyProductID=Convert.ToInt32(hdCompanyProductID);
                }
                else
                {CompanyProductID=db.tblcompanyproducts.Where(x => x.nProductID == ddlProduct && x.nCompanyID == ddlCompany).FirstOrDefault().nCompanyProductID;}
                if (id == null)
                {
                    if (db.tblproductattributemappers.Where(x => x.nCompanyProductID == CompanyProductID && x.nAttributeID == model.nAttributeID).Count() != 0)
                    {
                        TempData["Errmsg"] = "Addon value already exist with this product!";
                        return RedirectToAction("ProductAddons");
                    }

                    tblattributValMapper = new tblproductattributemapper();
                    tblattributValMapper.nAttributeID = model.nAttributeID;
                    tblattributValMapper.nCompanyProductID = CompanyProductID;
                    db.AddTotblproductattributemappers(tblattributValMapper);
                }
                else
                {
                    tblattributValMapper = db.tblproductattributemappers.Where(x => x.nMapperID == id).FirstOrDefault();
                    tblattributValMapper.nAttributeID = model.nAttributeID;
                    tblattributValMapper.nCompanyProductID = CompanyProductID;

                }

                db.SaveChanges();    //// Error comes here when it goes into else part(update mode) then executed here
                // deleting objects
                var objmapperdetails = db.tblproductattributemapperdetails.Where(x => x.nMapperID == tblattributValMapper.nMapperID).ToList();
                if (objmapperdetails != null && objmapperdetails.Count() > 0)
                {
                    foreach (var item in objmapperdetails)
                    {
                        db.tblproductattributemapperdetails.DeleteObject(item);
                    }
                }
                string[] arrAllStore = AllStorechk.Split(',');
                tblproductattributemapperdetail detail;
                foreach (var item in arrAllStore)
                {
                    Int64 _id = Convert.ToInt64(item);
                    detail = new tblproductattributemapperdetail();
                    detail.nMapperID = tblattributValMapper.nMapperID;
                    detail.nAttributeValueMasterID = _id;
                    detail.nPrice = db.tblattributevaluemasters.Where(x => x.nAttributeValueMasterID == _id).FirstOrDefault().nPrice;
                    db.AddTotblproductattributemapperdetails(detail);
                }

                db.SaveChanges();
                dbTran.Commit();
                TempData["Successmsg"] = "Saved successfully";
            }
            catch (Exception ex)
            {
                dbTran.Rollback();
                TempData["Errmsg"] = "Failed to save ";
                Common.TrapError(ex.StackTrace, "Attribute/ProductAddons");
            }
            finally
            {
                db.Connection.Close();
            }
            return RedirectToAction("ProductAddons");
        }

当我在编辑模式下打开任何记录并提交而不做任何更改时;那时它转到其他部分并使用旧值更新实体 tblattributValMapper 属性 nAttributeID 和 nCompanyProductID,当 db.SaveChanges();执行了 thorwing Error "Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries."

我在 google 上查看并应用了一些解决方案,例如 db.Refresh(RefreshMode.ClientWins,tblattributValMapper );

但这对我没有帮助。

我是这个项目的单身开发者,所以实体没有从任何其他地方更新,我还检查了主键值,它正在通过模型。 任何人都可以知道我在哪里做错了吗? 请参阅随附的屏幕截图。

如有任何帮助,我们将不胜感激。

我使用以下代码解决了这个问题。它在更新模式下对我有用。

  try
  { db.SaveChanges(); }
  catch (OptimisticConcurrencyException)
  {
  db.Refresh(RefreshMode.StoreWins, tblattributValMapper);
  db.SaveChanges();
  }

谢谢。