2017-09-04 36 views
0

我想在ASP.Net MVC更新单场5我得到错误:在ASP.Net更新单场MVC 5

我的代码:

MyJobs.PublishStatus = 1; 
    db.MyJobContext.Attach(MyJobs); 
    db.Entry(MyJobs).Property(x => x.PublishStatus).IsModified = true; 
    //db.Configuration.ValidateOnSaveEnabled = false; 
    db.SaveChanges(); 

我得到波纹管错误:

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.

没有关于此错误的职位,但我无法弄清楚: Entity Framework: "Store update, insert, or delete statement affected an unexpected number of rows (0)."

和在asp.net MVC更新单场:How to update only one field using Entity Framework?

+1

'MyJobs.PublishStatus = 1;'如果'MyJobs'来自LINQ结果集,可能已经更新'PublishStatus',这样当执行Attach时,就不会再有行被更新。 –

+0

如果您将'ValidateOnSaveEnabled'设置为false,则它将起作用。我可以看到你的代码你已经有了这一行,你为什么评论它? – Munzer

+0

我试过但没有工作 –

回答

0

我通过更新我的形式固定了这个问题,并增加了输入隐藏Id字段

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    @Html.HiddenFor(model => model.Id) 
    <div class="form-actions no-color"> 
     <input type="submit" value="Publish" class="btn btn-default" /> 
    </div> 
} 

希望这会帮助别人谁面临着同样的问题。