2011-07-21 90 views
0

我試圖做MVC musicstore教程和使用編輯時,部分6後,我得到這個錯誤:MVC音樂商店

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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OptimisticConcurrencyException: 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.

知道爲什麼這是什麼原因?

[HttpPost] 
public ActionResult Edit(Album album) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Entry(album).State = EntityState.Modified; 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); 
    ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); 

    return View(album); 
} 

有人腳手架

回答

-1

確保你打電話SaveChanges任何插入,更新或刪除在兩者之間。

+0

>'[HttpPost] 公共的ActionResult編輯(專輯的專輯) { 如果(ModelState.IsValid) { db.Entry(專輯).STATE = EntityState.Modified; db.SaveChanges(); return RedirectToAction(「Index」); } ViewBag.GenreId = new SelectList(db.Genres,「GenreId」,「Name」,album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists,「ArtistId」,「Name」,album.ArtistId); return查看(專輯);' 它是腳手架 – dave

+0

@dave請使用該代碼更新您的問題,而不是對答案進行評論。 –

+0

中間有保存變化。它沒有工作。我也有這個相同的問題。任何想法? –