【问题标题】:Store update, insert, or delete statement affected an unexpected number of rows (0).存储更新、插入或删除语句影响了意外数量的行 (0)。
【发布时间】:2019-10-09 15:51:12
【问题描述】:

我对 MVC 很陌生。我从数据库中检索了值并将其显示在视图中,我想进行更改并更新现有值。检索值没有问题,但是当我创建保存时,它在 db.changes 行返回错误:

存储更新、插入或删除语句影响了意外 行数 (0)。实体可能已被修改或删除 实体已加载。刷新 ObjectStateManager 条目。

我已经尝试了所有在 Edit.cshtml 中包含 @Html.HiddenFor(m => m.Question_ID) 但仍然没有运气。

我的 CreateQuestionViewModel:

public class CreateQuestionViewModel
    {
        [HiddenInput(DisplayValue = false)]
        public int Question_ID { get; set; }

        [HiddenInput(DisplayValue=false)]
        public int Survey_ID { get; set; }

        [DisplayName("Question Title")]
        public string Qext_Text { get; set; }

        [DisplayName("Question Type")]
        public string Question_Type { get; set; }

        public bool Mandatory { get; set; }

        public string Language { get; set; }
    }
}

我的编辑控制器:

  [HttpGet]
            public ActionResult Edit(int Question_ID = 0)
            {
                var viewModel = new CreateQuestionViewModel();
                viewModel.Question_ID = Question_ID;

                if (ModelState.IsValid)
                {
                    SURV_Question_Model surv_question_model = db.SURV_Question_Model.Find(Question_ID);
                    viewModel.Mandatory = surv_question_model.Question_Mandatory; 
                    viewModel.Question_Type = surv_question_model.Question_Type;

                    SURV_Question_Ext_Model surv_question_ext_model = db.SURV_Question_Ext_Model.FirstOrDefault(x => x.Qext_Question_ID.Equals(Question_ID));
                    viewModel.Qext_Text = surv_question_ext_model.Qext_Text;
                    viewModel.Language= surv_question_ext_model.Qext_Language;

                }
                return View(viewModel);
            }

        [HttpPost]
                public ActionResult Edit(CreateQuestionViewModel viewModel)
                {
                    if (ModelState.IsValid)
                    {
                        var question = new SURV_Question_Model();
                        question.Question_Mandatory = viewModel.Mandatory;
                        question.Question_Type = viewModel.Question_Type;

                        db.Entry(question).State = EntityState.Modified;
                        db.SaveChanges();

                        var question_ext = new SURV_Question_Ext_Model();
                        question_ext.Qext_Text = viewModel.Qext_Text;
                        question_ext.Qext_Language = viewModel.Language;

                        db.Entry(question_ext).State = EntityState.Modified;
                        db.SaveChanges();


                        return RedirectToAction("Edit", "SURV_Main", new { id = viewModel.Survey_ID });

                    }

请给我一些指导。 谢谢!

【问题讨论】:

  • 您创建一个新问题和 question_ext 并从传递的视图模型中分配值。所以如果您尝试保存更改,它将另存为新行,但在此之前您将它们的实体状态指定为已修改,因此系统不能修改..所以首先不要创建一个新的创建这样的东西并尝试var question = db.createviewquestionmodel.where(d=> d.question_id == viewmodel.question_id).firstordefault(); 然后将值分配给问题并尝试使用编辑模式保存
  • 感谢 Sachu 的指导,我试过了,效果很好。非常感谢您的反馈。您能否将其添加为答案,以便我可以将其标记为答案?再次感谢。
  • 很高兴听到它有帮助..我将其添加为答案..

标签: c# asp.net-mvc-4 razor


【解决方案1】:

您正在创建一个新的questionquestion_ext 并从传递的视图模型中分配值。因此,如果您尝试保存更改,它将另存为新行,但在此之前您将其中的entitystate 指定为modified,因此系统无法修改。这就是错误出现的原因。 所以首先不要创建一个新的创建这样的东西并尝试

     var question = db.createviewquestionmodel.where
    (d=> d.question_id == viewmodel.question_id).firstordefault(); 

    question.Question_Mandatory = viewModel.Mandatory;
   question.Question_Type = viewModel.Question_Type;
   db.Entry(question).State = EntityState.Modified;
   db.SaveChanges();

然后将值分配给问题并尝试使用编辑模式保存

【讨论】:

  • 再次感谢您的指导。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
相关资源
最近更新 更多