gpt4 book ai didi

c# - System.Data.Entity.Infrastructure.DbUpdateConcurrencyException。存储更新、插入或删除语句影响了意外的行数 (0)

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

模型这些是模型类,我正在使用 Entity Framework 。我正在使用这些模型来实现级联下拉列表。

  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; }

}

Controller / Action 在 HttpGet 中,我刚刚填充了级联下拉列表并且工作正常,现在我正在为此实现 Httppost。我想根据从下拉列表中选择的列表项存储 price,如果 price 已经存在,那么我想更新它。第一次我可以成功添加 price 但是第二次当我尝试更新它时我得到 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException 请任何人指导我如何处理这个。

[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;

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);
}

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

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