c# - How can I delete items from my asp.net database -
i'm trying out asp.net , i'm stumped system.data.entity.infrastructure.dbupdateconcurrencyexception
. exception occurs on line try save changes database in 'delete' action method.
here code edit method, works fine:
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"); }
and here code delete method not!
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 outcommented line above , current method of marking deletion result in same exception on line below. db.savechanges(); return redirecttoaction("index"); }
[httppost] public actionresult delete(movie movie) { var dbmovie = db.movies.find(movie.id); db.movies.remove(dbmovie); db.savechanges(); return redirecttoaction("index"); }
Comments
Post a Comment