System.Data.Entity.Infrastructure.DbUpdateConcurrencyException. Store update, insert, or delete statement affected an unexpected number of rows (0)

前提是你 提交于 2020-01-13 19:42:29

问题


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

    }

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;

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

View

  @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" />
}

回答1:


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



回答2:


We have found same error because we have used two trigger on insert and delete. we have delete trigger then issue is resolve our side.



来源:https://stackoverflow.com/questions/38180372/system-data-entity-infrastructure-dbupdateconcurrencyexception-store-update-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!