gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:19:28 24 4
gpt4 key购买 nike

希望你一切顺利。

请帮助我解决这个问题。我在 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 );

但这对我没有帮助。

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

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

最佳答案

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

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

谢谢。

关于c# - 存储更新、插入或删除语句影响了意外数量的行 (0),,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37812665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com