ASP.NET MVC EntityState.Modified crash

I’ve been struggling with this error for the past day or so. I’ve debugged it a number of times and each time it crashes on this line:

db.Entry(article).State = EntityState.Modified;

Commenting out the line gets rid of the crash, but the record doesn’t get updated in the database.

Here’s the whole code:

    [HttpPost]
    [Authorize]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "ArticleId,MainTitle,SubTitle,DatePublished,Content,ImagePath,UserAccountId")] Article article)
    {
        if (ModelState.IsValid)
        {
            // This is necessary so that we can preserve the original publish date:
            var originalArticle = db.Articles.Where(a => a.ArticleId == article.ArticleId).First();
            article.DatePublished = originalArticle.DatePublished;

            // This is necessary so that we can preserve who was the original poster:
            article.UserAccountId = originalArticle.UserAccountId;


            //db.Entry(article).State = EntityState.Modified; <---- Crashes
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.UserAccountId = new SelectList(db.UserAccounts, "UserAccountId", "FirstName", article.UserAccountId);
        return View(article);
    }

I would really appreciate it if someone could help me out with this thing! 🙂

EDIT: This is the exception being thrown:

“An exception of type ‘System.InvalidOperationException’ occurred in EntityFramework.dll but was not handled in user code

Additional information: Attaching an entity of type ‘TheNewsBETA.Models.Article’ failed because another entity of the same type already has the same primary key value. This can happen when using the ‘Attach’ method or setting the state of an entity to ‘Unchanged’ or ‘Modified’ if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the ‘Add’ method or the ‘Added’ entity state to track the graph and then set the state of non-new entities to ‘Unchanged’ or ‘Modified’ as appropriate.”

Answer

Alright, the thing that fixed it was creating a new context within the Edit method and applying the same operations as before to the new context(so far I tried to apply them to a context that was a private field for the whole ArticleController class). This was suggested by Nilesh, and I even didn’t have to use an attach. I also tried adding db.Articles.Attach(article) before the point where it threw an exception, but that didn’t seem to help. I’d like to thank everybody who gave their ideas and suggestions!

Here’s how the code looks right now:

if (ModelState.IsValid)
        {
            var newContext = new ApplicationDbContext();


            // This is necessary so that we can preserve the original publish date:
            var originalArticle = db.Articles.Where(a => a.ArticleId == article.ArticleId).First();
            article.DatePublished = originalArticle.DatePublished;

            // This is necessary so that we can preserve who was the original poster:
            article.UserAccountId = originalArticle.UserAccountId;

            newContext.Entry(article).State = EntityState.Modified;
            newContext.SaveChanges();
            /*  Old code that didn't work:
            db.Entry(article).State = EntityState.Modified;
            db.SaveChanges();
            */
            return RedirectToAction("Index");
        }