文章内容
2017/3/28 9:59:06,作 者: 黄兵
Store update, insert, or delete statement affected an unexpected number of rows (0)
今天在写代码的时候,出现如下错误:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
查阅相关资料之后解决问题,解决方法记录下来,方便后人遇到同样错误能够解决,解决方案如下:
首先看一下前台传上来的数据:
注意橘红色框框那里的标记,之后我们看一下数据库对应的数据:
这里可以明显的看到,FirstProId是0,而这里要的是8,也就是说FirstProId没有数据上传,
修改前后端代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //传入的Id为二级目录Id [HttpGet] public ActionResult EditSecond( int Id) { using (UnitOfWork uow= new UnitOfWork()) { var getSecond = uow.SecondProjectRepository.Get(x => x.Id == Id); var SecondViewModel = new EditProjectViewModel(); SecondViewModel.Id = getSecond.Id; SecondViewModel.SecondName = getSecond.SecondProName; SecondViewModel.FirstProId = getSecond.FirstProId; return View(SecondViewModel); } } |
向前端传输必要的数据,之后前端隐藏发送到后端,前端代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @ using (Html.BeginForm( "EditSecond" , "Manage" , FormMethod.Post)) { @Html.AntiForgeryToken() @Html.HiddenFor(Model=>Model.FirstProId) @Html.HiddenFor(Model=>Model.Id) <div class = "ibox-content" > <div class = "form-group" > @Html.LabelFor(model => model.SecondName, htmlAttributes: new { @ class = "control-label col-md-2" }) <div class = "col-sm-10" > @Html.TextBoxFor(model => model.SecondName, new { @ class = "form-control" , required = "" , maxlength = "50" }) @Html.ValidationMessageFor(model => model.SecondName, "" , new { @ class = "text-danger" }) </div> </div> <div class = "hr-line-dashed" ></div> <div class = "form-group" > <div class = "col-sm-4 col-sm-offset-2" > <button class = "btn btn-primary" type= "submit" >保存内容</button> </div> </div> </div> } |
注意:这两句:
@Html.HiddenFor(Model=>Model.FirstProId)
@Html.HiddenFor(Model=>Model.Id)
修改完毕看一下后台抓取的数据,如下图:
好了数据库所需要的数据都一一对应上了,之后更新数据就不会报错了。
问题解决。有什么问题给我在下面留言。我每天看博客,第一时间回。
参考网址:
评论列表