Thanks @ToughMan but unfortunately it does not work, its not that straightforward. Can you give me an example on how it would work?
[HttpPost]
public ActionResult Edit(Department department)
{
try
{
if (ModelState.IsValid)
{
db.Entry(department).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var databaseValues = (Department)entry.GetDatabaseValues().ToObject();
var clientValues = (Department)entry.Entity;
if (databaseValues.Name != clientValues.Name)
ModelState.AddModelError("Name", "Current value: "
+ databaseValues.Name);
if (databaseValues.Budget != clientValues.Budget)
ModelState.AddModelError("Budget", "Current value: "
+ String.Format("{0:c}", databaseValues.Budget));
if (databaseValues.StartDate != clientValues.StartDate)
ModelState.AddModelError("StartDate", "Current value: "
+ String.Format("{0:d}", databaseValues.StartDate));
if (databaseValues.InstructorID != clientValues.InstructorID)
ModelState.AddModelError("InstructorID", "Current value: "
+ db.Instructors.Find(databaseValues.InstructorID).FullName);
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");
department.Timestamp = databaseValues.Timestamp;
}
catch (DataException)
{
//Log the error (add a variable name after Exception)
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
}
ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "FullName", department.InstructorID);
return View(department);
}
Thanks a lot Decker for your reply, this I have already done beacuse person entity is working for database and client values.
The issues that was and which stays is why should i load the related entity separately for databasevalues using Find. In your example if department has a foreign key Department Head coming from a table say people, I should be able to refer to People entity
for department head when doing
var databaseValues =(Department)entry.GetDatabaseValues().ToObject();
databasevalues.people.departmenhead, this gives null
I really appreciate you are taking out time to reply on this query.
The solution given by @toughman works as a workaround using find method. Thanks @toughman for refering me to the link.
GauravRajput
Member
6 Points
15 Posts
Re: How to load multiple related entities in DbUpdateConcurrencyException
Nov 04, 2012 04:50 AM|LINK
Thanks @ToughMan but unfortunately it does not work, its not that straightforward. Can you give me an example on how it would work?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to load multiple related entities in DbUpdateConcurrencyException
Nov 04, 2012 05:34 AM|LINK
[HttpPost] public ActionResult Edit(Department department) { try { if (ModelState.IsValid) { db.Entry(department).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } catch (DbUpdateConcurrencyException ex) { var entry = ex.Entries.Single(); var databaseValues = (Department)entry.GetDatabaseValues().ToObject(); var clientValues = (Department)entry.Entity; if (databaseValues.Name != clientValues.Name) ModelState.AddModelError("Name", "Current value: " + databaseValues.Name); if (databaseValues.Budget != clientValues.Budget) ModelState.AddModelError("Budget", "Current value: " + String.Format("{0:c}", databaseValues.Budget)); if (databaseValues.StartDate != clientValues.StartDate) ModelState.AddModelError("StartDate", "Current value: " + String.Format("{0:d}", databaseValues.StartDate)); if (databaseValues.InstructorID != clientValues.InstructorID) ModelState.AddModelError("InstructorID", "Current value: " + db.Instructors.Find(databaseValues.InstructorID).FullName); ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); department.Timestamp = databaseValues.Timestamp; } catch (DataException) { //Log the error (add a variable name after Exception) ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator."); } ViewBag.InstructorID = new SelectList(db.Instructors, "InstructorID", "FullName", department.InstructorID); return View(department); }http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
GauravRajput
Member
6 Points
15 Posts
Re: How to load multiple related entities in DbUpdateConcurrencyException
Nov 04, 2012 06:06 AM|LINK
Thanks a lot Decker for your reply, this I have already done beacuse person entity is working for database and client values.
The issues that was and which stays is why should i load the related entity separately for databasevalues using Find. In your example if department has a foreign key Department Head coming from a table say people, I should be able to refer to People entity for department head when doing
var databaseValues = (Department)entry.GetDatabaseValues().ToObject();
databasevalues.people.departmenhead, this gives null
I really appreciate you are taking out time to reply on this query.
The solution given by @toughman works as a workaround using find method. Thanks @toughman for refering me to the link.
Thanks All.
Cathy Mi - M...
Member
741 Points
165 Posts
Microsoft
Re: How to load multiple related entities in DbUpdateConcurrencyException
Nov 21, 2012 07:46 PM|LINK
Hi,
Were you able to work this out?
If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs: http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone.
Thanks,
Cathy Miller
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to load multiple related entities in DbUpdateConcurrencyException
Nov 30, 2012 12:29 AM|LINK
Sorry, I've recently submitted a thread http://forums.asp.net/t/1860461.aspx/2/10?p=True&t=634898176251117433
But the questioner wanna solved soon.
He lets me ask the process……:D