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.
存储区更新、插入或删除语句影响到了意外的行数({0})。实体在加载后可能被修改或删除。刷新 ObjectStateManager 项。
如下ASP.NET MVC 范例代码,使用Entity Framework 来更新数据记录。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[HttpPost] public ActionResult Edit(Album album) { if (ModelState.IsValid) { storeDB.Entry(album).State = EntityState.Modified; storeDB.SaveChanges(); return RedirectToAction( "Index" ); } ViewBag.Genres = new SelectList(storeDB.Genres, "GenreId" , "Name" , album.GenreId); ViewBag.Artists = new SelectList(storeDB.Artists, "ArtistId" , "Name" , album.ArtistId); return View(album); } |
出现上述异常的原因,一般是由于Model 或 Entity 实体对象的Key 值没有赋值,无法在数据库对应表中找到对应的记录,也就无法执行更新操作。
解决办法是在View 视图中,添加一个隐藏的Key值,如下所示:
1
|
@Html.HiddenFor(model => model.AlbumId) |
摘自 http://www.entlib.net/?p=136