【问题标题】:ASP.NET MVC EntityState.Modified crashASP.NET MVC EntityState.Modified 崩溃
【发布时间】:2016-07-14 10:53:18
【问题描述】:

过去一天左右,我一直在为这个错误而苦苦挣扎。我已经调试了很多次,每次都在这条线上崩溃:

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

注释掉该行可以消除崩溃,但数据库中的记录不会更新。

这是整个代码:

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

如果有人能帮我解决这个问题,我将不胜感激! :)

编辑:这是抛出的异常:

“EntityFramework.dll 中出现‘System.InvalidOperationException’类型的异常,但用户代码中没有处理

附加信息:附加类型为“TheNewsBETA.Models.Article”的实体失败,因为同一类型的另一个实体已经具有相同的主键值。如果图中的任何实体具有冲突的键值,则在使用“附加”方法或将实体的状态设置为“未更改”或“已修改”时,可能会发生这种情况。这可能是因为某些实体是新实体,尚未收到数据库生成的键值。在这种情况下,使用 'Add' 方法或 'Added' 实体状态来跟踪图形,然后将非新实体的状态设置为 'Unchanged' 或 'Modified' 视情况而定。”

【问题讨论】:

  • “它崩溃了”是无法回答的。检查实际异常及其内部异常,并研究这些异常。
  • 请原谅我的无知,抛出的异常是:“EntityFramework.dll 中发生'System.InvalidOperationException' 类型的异常,但未在用户代码中处理”
  • edit 将其纳入您的问题,但在研究之前不要。
  • 如果您使用相同的上下文从数据库中提取记录,它将崩溃。我认为您需要创建一个新的上下文并将文章附加到它。
  • 您是否尝试在该行代码之前将对象附加到上下文? db.Articles.Attach(article)

标签: c# sql asp.net-mvc entity-framework


【解决方案1】:

好的,修复它的方法是在 Edit 方法中创建一个新上下文,并将与以前相同的操作应用于新上下文(到目前为止,我尝试将它们应用于整个 ArticleController 的私有字段的上下文班级)。这是 Nilesh 建议的,我什至不必使用附件。 我还尝试在它引发异常之前添加db.Articles.Attach(article),但这似乎没有帮助。 我要感谢所有提出想法和建议的人!

下面是代码现在的样子:

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");
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-12
    • 2014-06-30
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多