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

希望你一切顺利。

请帮我解决这个问题。我在 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();执行它时出现错误“存储更新、插入或删除语句影响了意外数量的行 (0)。实体可能已在加载实体后被修改或删除。刷新 ObjectStateManager 条目。”

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

但这对我没有帮助。

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

任何帮助将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

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

    谢谢。

    【讨论】:

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