Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 774803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:11:25+00:00 2026-05-14T19:11:25+00:00

I’m wanting to do a simple edit form for our Issue Tracking app. For

  • 0

I’m wanting to do a simple edit form for our Issue Tracking app. For simplicity, the HttpGet Edit action looks something like this:

    // Issues/Edit/12
    public ActionResult Edit(int id)
    {
        var thisIssue = edmx.Issues.First(i => i.IssueID == id);
        return View(thisIssue);
    }

and then the HttpPost action looks something like this:

    [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 => 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);
    }

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 SaveChanges() but attach my thisIssue to the context. I tried to call edmx.Issues.Attach(thisIssue) but I get

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.

How do I handle concurrency in MVC with EF and/or how do I properly Attach my edited object to the context?

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:11:26+00:00Added an answer on May 14, 2026 at 7:11 pm

    What you are doing is tricky, but can be made to work. Let’s presume your timestamp field is called ConcurrencyToken. Obviously, you must include this value in the View and submit it with your form. But you can’t simply assign that to the value to thisIssue.ConcurrencyToken in the POST because the EF will remember both the “old” value (the value you fetched from the DB with your call to Single() as well as the “new” value (from your form) and use the “old” value in the WHERE clause. So you need to lie to the EF and assign the correct value. Try this:

        var thisIssue = edmx.Issues.Single(c => c.IssueID == id);
        TryUpdateModel(thisIssue); // assign ConcurrencyToken
        var ose = Context.ObjectStateManager.GetObjectStateEntry(entityToUpdate);
        ose.AcceptChanges();       // pretend object is unchanged
        TryUpdateModel(thisIssue); // assign rest of properties
    

    You can optimize this by binding only ConcurrencyToken instead of calling TryUpdateModel twice, but this should get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.