2
希望你做得很好。商店更新,插入或刪除語句影響了意想不到的行數(0),
請幫助我解決這個問題。我在ASP.NET mvc web Application中使用下面的代碼。
[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錯誤「存儲更新,插入或刪除語句影響了意外數量的行(0)。實體可能已被修改或刪除,因爲實體已加載。刷新ObjectStateManager條目。」
我檢查它在谷歌和應用像db.Refresh(RefreshMode.ClientWins,tblattributValMapper );
一些解決方案,但它並不能幫助我。
我是這個項目的單一開發人員,所以實體沒有從任何其他地方更新,我還檢查了主鍵值,它是通過模型傳遞的。 任何人都可以有任何想法,我犯了錯誤。 請參閱附件屏幕截圖。
任何幫助將不勝感激。