I was working on a sample MVC 2 project, and wanted to post about the approach I took for updates.  I’m not saying that this is “definitely the way you have to do” any updates to your database.  I was evaluating the approach people were taking when it comes to attaching objects to the context.  MVC’s model binding allows for a posted action method to receive a LINQ to Entity object.  MVC takes the form values posted to the server and, using reflection, supplies the values to the properties of the object, if it can effectively match it.  This is one of the challenges, ensuring that the form values match so the binding works correctly, not so much for one object, but more for complex views binding hierarchies of objects.  But I digress.

 When MVC binds to my model, the object is unattached; this is because it knows nothing about my object related to the context.  With Entity Framework 4, I decided use the AttachTo method as the following:

[HttpPost]
public ActionResult ModifyStory(ProjectStory story)
{
 if (!ModelState.IsValid)
  return View();

 var context = new ProjectTrackingObjectContext();

 if (story.ProjectStoryKey > 0)
  context.AttachTo(context.ProjectStories.EntitySet.Name, story);
 else
 {
  story.CreatedDate = DateTime.Now;
  context.ProjectStories.AddObject(story);
 }

 context.SaveChanges();

 return RedirectToAction(“ModifyStory”, new { id = story.ProjectKey, story = story.ProjectStoryKey });
}

It is pretty convenient; the generated entity sets have a name property that can be used  with AttachTo (you have to supply the name of the entity set; this equaled the type name in EF 1, but with the pluralization of entity names feature in 4, you can’t bank on that).  So attaching the object with the supplied key (if there is a valid key, otherwise an insert happens) works pretty well.