Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Concurrency with RowVersion in Edit Action
    primarykey
    data
    text
    <p>I'm wanting to do a simple edit form for our Issue Tracking app. For simplicity, the HttpGet Edit action looks something like this:</p> <pre><code> // Issues/Edit/12 public ActionResult Edit(int id) { var thisIssue = edmx.Issues.First(i =&gt; i.IssueID == id); return View(thisIssue); } </code></pre> <p>and then the HttpPost action looks something like this:</p> <pre><code> [HttpPost] public ActionResult Edit(int id, FormCollection form) { // this is the dumb part where I grab the object before I update it. // concurrency is sidestepped here. var thisIssue = edmx.Issues.Single(c =&gt; c.IssueID == id); TryUpdateModel(thisIssue); if (ModelState.IsValid) { edmx.SaveChanges(); TempData["message"] = string.Format("Issue #{0} successfully modified.", id); return RedirectToAction("Index"); } return View(thisIssue); } </code></pre> <p>Which works wonderfully. However, the concurrency check doesn't work because in the Post, I'm re-retreiving the current entity right before I attempt to update it. However, with EF, I don't know how to use the fanciness of <code>SaveChanges()</code> but attach my <code>thisIssue</code> to the context. I tried to call <code>edmx.Issues.Attach(thisIssue)</code> but I get </p> <pre><code>The object cannot be attached because it is already in the object context. An object can only be reattached when it is in an unchanged state. </code></pre> <p>How do I handle concurrency in MVC with EF and/or how do I properly <code>Attach</code> my edited object to the context?</p> <p>Thanks in advance</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload