System.Data.Entity.Infrastructure.DbUpdateConcurrencyException.存储更新,插入或删除语句影响了意外的行数(0) [英] System.Data.Entity.Infrastructure.DbUpdateConcurrencyException. Store update, insert, or delete statement affected an unexpected number of rows (0)
问题描述
模型 这些是模型类,我正在使用实体框架.我正在使用这些模型来实现级联下拉列表.
Models These are model classes and I am using Entity Framework. I am using these models to implement cascaded drop down list.
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.我想根据下拉列表中的选定列表项存储价格,并且如果价格已经存在,那么我想对其进行更新.第一次我可以成功添加 price ,但是第二次尝试更新时,会出现 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ,请有人可以指导我如何处理这个.
Controller/Actions In HttpGet I just populated cascaded dropdown list and working fine now I am implementing Httppost for this. I want to store price depending upon selected list items from dropdown list and if the price already exists then I want to update it. First time I can add price successfully but second time when I am trying to update it I am getting System.Data.Entity.Infrastructure.DbUpdateConcurrencyException Please can anybody guide me how to handle this.
[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.DbUpdateConcurrencyException",但未在用户代码中处理. 附加信息:存储更新,插入或删除语句影响了意外的行数(0).自加载实体以来,实体可能已被修改或删除.刷新ObjectStateManager条目.
Exception occurs here in line db.SaveChanges(); 'System.Data.Entity.Infrastructure.DbUpdateConcurrencyException' occurred in EntityFramework.dll but was not handled in user code. Additional information: 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.
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);
}
这篇关于System.Data.Entity.Infrastructure.DbUpdateConcurrencyException.存储更新,插入或删除语句影响了意外的行数(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!