I am currently implementing the POST edit action method as follow:-
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
Book c = erepository.GetBook(id);
if (TryUpdateModel(c))
{
erepository.Save();
return RedirectToAction("Details", new { id = c.ID });
}
// code does here
But i have saw in the music store example which uses the Scaffolding tool to implement similar edit scenario using EntityState.Modified such as ;
[HttpPost]
public ActionResult Edit(Album album)
{
if (ModelState.IsValid)
{
db.Entry(album).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction(“Index”);
}
My questions are :
1. will the second approach provides better performance as i will not have to search for the object before editing it ?
2. i tiered to implement the second scenario inside my application but the Runtime intellesense did not display
Entry after my Dbcontext object inside the repository class ; although i have addede using System.Data.Entity.Infrastructure;
Perhaps you're using Code First and the sample is not (i.e. is it using edmx stuff)? What's you're doing is fine. There won't be any performance hit and the TryUpdateModel will take care of MVC supported validators.
"If I can see further than anyone else, it is only because I am standing on the shoulders of giants."blog: www.heartysoft.com twitter: @ashic
Marked as answer by HeartattacK on Jan 28, 2012 10:38 AM
Perhaps you're using Code First and the sample is not (i.e. is it using edmx stuff)?
could u please explain your point; as i am using database first apprach using entity framework which i think is also valid ? am i doing something wrong?
secondly is it still possible to use thing similat to
db.Entry(album).State=EntityState.Modified instead of my original approach, even if i am using Database
first approach ?
BR
there is a minor difference between your code and the scaffold code.
scaffold:
binder - create new model
binder - tryupdate model
controller - add model to dbcontext
controler - maually mark model context as require update
controller -save
your code:
controller - get model from db into context
controller - tryupdate context model
controller - save
the difference is that in your code, all the data is read from the database, and any passed form fields replace the data. if change tracking is enabled, then the modiified state will be set by the change, if you use POCO (database first) you will propably
need to set modified manually anyway unless you implement change tracking.
so you code handles partial forms, and auto detect of changes (if you implement change tracking).
note: both samples do not detect if another user has made changes to the data and just overwrite it.
if change tracking is enabled, then the modiified state will be set by the change, if you use POCO (database first) you will propably need to set modified manually anyway unless you implement change tracking.
I do not know exactly what do you mean by change tracking is enabled? i am currently handling concurrency by defining tracking property and Timestamp attribute at the model class,
and then displaying an error message in case the original timestamp value was not found during the update process (is it different from what u r mentioning)?
johnjohn1231...
Participant
922 Points
871 Posts
Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 02:59 AM|LINK
I am currently implementing the POST edit action method as follow:-
[HttpPost] public ActionResult Edit(int id, FormCollection collection) { Book c = erepository.GetBook(id); if (TryUpdateModel(c)) { erepository.Save(); return RedirectToAction("Details", new { id = c.ID }); } // code does hereBut i have saw in the music store example which uses the Scaffolding tool to implement similar edit scenario using EntityState.Modified such as ;
[HttpPost] public ActionResult Edit(Album album) { if (ModelState.IsValid) { db.Entry(album).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction(“Index”); }My questions are :
1. will the second approach provides better performance as i will not have to search for the object before editing it ?
2. i tiered to implement the second scenario inside my application but the Runtime intellesense did not display Entry after my Dbcontext object inside the repository class ; although i have addede using System.Data.Entity.Infrastructure;
BR
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 05:13 AM|LINK
Perhaps you're using Code First and the sample is not (i.e. is it using edmx stuff)? What's you're doing is fine. There won't be any performance hit and the TryUpdateModel will take care of MVC supported validators.
blog: www.heartysoft.com
twitter: @ashic
johnjohn1231...
Participant
922 Points
871 Posts
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 11:21 AM|LINK
could u please explain your point; as i am using database first apprach using entity framework which i think is also valid ? am i doing something wrong?
secondly is it still possible to use thing similat to db.Entry(album).State = EntityState.Modified instead of my original approach, even if i am using Database first approach ?
BR
HeartattacK
All-Star
55262 Points
5917 Posts
Moderator
MVP
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 11:58 AM|LINK
No...what you're doing is fine. I'd avoid setting the state like that since you'll be tying your code to EF. It should work but I'd avoid it.
blog: www.heartysoft.com
twitter: @ashic
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 03:36 PM|LINK
there is a minor difference between your code and the scaffold code.
scaffold:
binder - create new model
binder - tryupdate model
controller - add model to dbcontext
controler - maually mark model context as require update
controller -save
your code:
controller - get model from db into context
controller - tryupdate context model
controller - save
the difference is that in your code, all the data is read from the database, and any passed form fields replace the data. if change tracking is enabled, then the modiified state will be set by the change, if you use POCO (database first) you will propably need to set modified manually anyway unless you implement change tracking.
so you code handles partial forms, and auto detect of changes (if you implement change tracking).
note: both samples do not detect if another user has made changes to the data and just overwrite it.
johnjohn1231...
Participant
922 Points
871 Posts
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 17, 2012 11:41 PM|LINK
I do not know exactly what do you mean by change tracking is enabled? i am currently handling concurrency by defining tracking property and Timestamp attribute at the model class, and then displaying an error message in case the original timestamp value was not found during the update process (is it different from what u r mentioning)?
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: Using EntityState.Modified Versus passing the ID when editing an object
Jan 18, 2012 02:22 AM|LINK
if change tracking is not enabled in the dbcontext, then
get model
try update
save changes
will not perform a save, because it will not know the model was updated. you will need to manually make the entity state modified.
PakXoxer
Member
393 Points
91 Posts
Re: Using EntityState.Modified Versus passing the ID when editing an object
Apr 09, 2012 03:55 AM|LINK
Finally, what is the prefered method of doing this activity? either scofolding or the other one?