System.Data.Entity.Infrastructure.DbUpdateConcurrentExceptio
本文关键字:DbUpdateConcurrentExceptio Infrastructure Entity Data System | 更新日期: 2023-09-27 17:54:38
模型这些是模型类,我使用的是实体框架。我正在使用这些模型来实现级联下拉列表。
public class League
{
public int Id { get; set; }
public string League1 { get; set; }
public string Icon { get; set; }
public virtual ICollection<LeagueDivision> LeagueDivisions { get; set; }
}
public class LeagueDivision
{
public int Id { get; set; }
public Nullable<int> LeagueId { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public virtual League League { get; set; }
}
public partial class CalculatorPrice
{
public int Id { get; set; }
public int LeagueId { get; set; }
public int LeagueDivisionId { get; set; }
public Nullable<decimal> Price { get; set; }
}
public class ViewModelForHostBooster
{
[Required(ErrorMessage = "Please enter price")]
[Display(Name = "Price")]
public decimal Price { get; set; }
[Required(ErrorMessage = "Please select a league")]
[Display(Name = "League")]
public int? SelectedLeague { get; set; }
[Required(ErrorMessage = "Please select a league division")]
[Display(Name = "League Division")]
public int? SelectedLeagueDivision { get; set; }
public SelectList LeagueList { get; set; }
public SelectList LeagueDivisionList { get; set; }
}
控制器/操作在HttpGet中,我刚刚填充了级联下拉列表,现在工作正常,我正在为此实现Httppost。我想存储价格,具体取决于从下拉列表中选择的列表项,如果定价已经存在,则我想更新它。第一次我可以成功添加价格,但第二次当我尝试更新它时,我得到了System.Data.Entity.Infrastructure.DbUpdateConcurrentException请任何人指导我如何处理此问题。
[HttpPost]
public ActionResult IndexDropDown(ViewModelForHostBooster model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
else
{
HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
CalculatorPrice calculatePrice = new CalculatorPrice();
var calculatePriceExistsOrNot = db.CalculatorPrices
.Where(x => x.LeagueId == model.SelectedLeague
&&
x.LeagueDivisionId == model.SelectedLeagueDivision).ToList();
if (calculatePriceExistsOrNot.Count > 0)
{
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
db.Entry(calculatePrice).State = EntityState.Modified;
这里的db行出现异常。SaveChanges((EntityFramework.dll中出现"System.Data.Entity.Infrastructure.DbUpdateConcurrentException",但未在用户代码中处理。其他信息:存储更新、插入或删除语句影响了意外的行数(0(。自加载实体以来,实体可能已被修改或删除。刷新ObjectStateManager条目。
db.SaveChanges();
}
else
{
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
db.CalculatorPrices.Add(calculatePrice);
db.SaveChanges();
}
}
ConfigureViewModel(model);
return View(model);
}
查看
@using (Html.BeginForm("IndexDropDown", "DropDown", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<div>
@Html.LabelFor(m => m.Price, new { @class ="control-lebel"})
@Html.TextBoxFor(m => m.Price, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.Price)
</div>
<div>
@Html.LabelFor(m => m.SelectedLeague ,new { @class ="control-lebel"})
@Html.DropDownListFor(m => m.SelectedLeague, Model.LeagueList, new { @class = "form-control"})
@Html.ValidationMessageFor(m => m.SelectedLeague)
</div>
<div>
@Html.LabelFor(m => m.SelectedLeagueDivision ,new { @class ="control-lebel"})
@Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
</div>
<input type="submit" value="save" />
}
[HttpPost]
public ActionResult IndexDropDown(ViewModelForHostBooster model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
else
{
HostBoostersDBEntities2 db = new HostBoostersDBEntities2();
CalculatorPrice calculatePrice = db.CalculatorPrices
.Where(x => x.LeagueId == model.SelectedLeague
&&
x.LeagueDivisionId == model.SelectedLeagueDivision).FirstOrDefault();
if (calculatePrice != null)
{
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
//db.Entry(calculatePrice).State = EntityState.Modified;
db.SaveChanges();
}
else
{
calculatePrice = new CalculatorPrice();
calculatePrice.LeagueId = (int)model.SelectedLeague;
calculatePrice.LeagueDivisionId = (int)model.SelectedLeagueDivision;
calculatePrice.Price = model.Price;
db.CalculatorPrices.Add(calculatePrice);
db.SaveChanges();
}
}
ConfigureViewModel(model);
return View(model);
}
我们发现了同样的错误,因为我们在插入和删除时使用了两个触发器。我们有删除触发器,然后问题是解决我们这边。
有Id问题。
当我从一个视图重定向到另一个视图时,Id不知怎么消失了。
因此,我在一个方法中捕获了正确的Id,然后将用户重定向到编辑方法。为了指定,在第一个视图("索引"(中,我使用ActionLink将用户重定向到捕获Id的方法,然后,在捕获正确的Id后,将用户重定向至编辑方法。这可能不是最好的解决方案,但它对我有效
索引视图
@Html.ActionLink("Capture Id", "CauptureId", new { Model.Id }, null)
捕获Id
public ActionResult CauptureId(int? id)
{
Class nameOfClass = context.dbRow.Where(i => i.Id == id).FirstOrDefault();
return RedirectToAction("Edit", new { id = editIngredienser.Id});
}
编辑方法
public ActionResult Edit(int? id)
{
if (id == null)
{
return RedirectToAction("Index");
}
Class nameOfClass = db.dbRow.Find(id);
return View(nameOfClass);
}
[HttpPost]
public ActionResult Edit(Class nameOfClass)
{
db.Entry(nameofClass).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
结论
问题:我遇到的问题是,我试图更新数据库中Id为null的行,因为它消失了。
解决方案:我使用了一种方法来捕获正确的Id,以便能够更新数据库中的正确行。
正如我上面提到的,到目前为止,这不是最好的做法。但对我来说,这是一个暂时的解决方案。我希望你能找到它的一些用途。