2016-04-22 27 views
1

私はasp.netを試しています。私はSystem.Data.Entity.Infrastructure.DbUpdateConcurrencyExceptionで困惑しています。例外は、「削除」アクションメソッドで自分のデータベースへの変更を保存しようとする行で発生します。ここで私のasp.netデータベースから項目を削除する方法

は完全に正常に動作編集方法、のための私のコードです:

 public ActionResult Edit(int id) 
     { 
      Movie movie = db.Movies.Find(id); 
      if (movie == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(movie); 
     } 

     [HttpPost] 
     public ActionResult Edit(Movie movie) 
     { 
       db.Entry(movie).State = System.Data.Entity.EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
     } 

そして、ここにはありません、私のdeleteメソッドのコードです!

 public ActionResult Delete(int id) 
     { 
      Movie movie = db.Movies.Find(id); 
      if (movie == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(movie); 
     } 

     [HttpPost] 
     public ActionResult Delete(Movie movie) 
     { 
      db.Movies.Attach(movie); 
      db.Movies.Remove(movie); 
      //db.Entry(movie).State = System.Data.Entity.EntityState.Deleted; 
      //both the outcommented line above and the current method of marking for deletion result in the same exception on the line below. 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
+0

データベースのタイプを知って参考になる:dBaseのは、MySQL、SQLServerのは? – Velocibadgery

+0

@Aaron SQL Server Expressをインストールしました – user2651804

+0

可能なdup。 https://stackoverflow.com/questions/28660708/intermittent-system-data-entity-infrastructure-dbupdateconcurrencyexception –

答えて

1
[HttpPost] 
    public ActionResult Delete(Movie movie) 
    { 
     var dbMovie = db.Movies.find(movie.id); 
     db.Movies.Remove(dbMovie); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
+0

この結果、 'db.Movies.Remove(dbMovie)'でNullPointerExceptionが発生します。私が読むことができる限り、 'Remove()'関数は 'Movie'エンティティを期待しています。 – user2651804

+0

dbMovieは動画タイプである必要があります。 movie.idがnullの場合がありますか? – kravits88

+0

うーん..ええ。私は自分自身でその価値を宣言しません。しかし、Deleteメソッドが引数として削除するムービーを渡されたときに、なぜムービーを識別するために 'movie.id'を使用する必要がありますか?私はあなたのコードを理解していません。 – user2651804

関連する問題