I am a tad befuddled. I can’t reason why the following works:
AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues) {
Dinner dinner = dinnerRepository.GetDinner(id);
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
The sample is taken from Scott Guthrie’s NerdDinner walkthrough, where dinnerRepository.Save() is defined as:
public void Save() {
db.SubmitChanges();
}
And dinnerRepository.GetDinner(id) is defined as follows:
public Dinner GetDinner(int id) {
return db.Dinners.SingleOrDefault(d => d.DinnerID == id);
}
Ie, how does the the db NerdDinnerDataContext “know” to save the dinner object?
There must be a hole in my understanding of Linq To SQL, but blowed if I can pin point it. OK, so the dinner object has an id, but what tells db that there are changes to submit for that specific record with that Id?
I just can’t see it. Must be the World Cup…
I can only think that the DataContext object, db, keeps a reference to the dinner object that was got using the GetDinner method call. But… It all feels a bit ‘magical’
Andrew
You should look at the generated code for the LINQ to SQL classes. You’ll see that the property setters and getters for the classes contain change tracking so that the next time the repository is saved, the proper SQL statements are generated to commit any mutations made the the objects.