This works:
public ActionResult Save(int id, string name)
{
var profile = _profileRepository.GetById(id);
profile.Name = name;
_profileRepository.Save(profile); //this calls SaveOrUpdate()
//session.Commit() gets called in global.asax on Application_EndRequest()
//profile.Name is changed in the database
}
Since my actual problem is more complicated than this simple example, I'd like to get the profile from the repo, use TryUpdateModel on the profile to update any changes, and then save. However, when I introduce TryUpdateModel into the mix, it fails with a NonUniqueObjectException (a different object with the same identifier value was already associated with the session):
public ActionResult Save(int id)
{
var profile = _profileRepository.GetById(id);
TryUpdateModel(profile); //this works from the looks of it
_profileRepository.Save(profile); //this also appears to work;
//fails on session.Commit()
//nothing changed in database
}
A "different" object with the same identifier value?? It looks as though TryUpdateModel is disconnecting my profile object from the session. Any thoughts?