UPDATE 语句与 FOREIGN KEY 约束 MVC Web 应用程序冲突

The UPDATE statement conflicted with the FOREIGN KEY constraint MVC Web Application

上下文:

我希望用户只更新条目的一部分 [AppStatus]。但是,当我测试它时,我得到了主题中提到的错误。我见过类似的问题,并尝试执行他们概述的一些步骤(例如,对 FK 使用 CASCADE)。但它没有用。所以这可能是我搞砸了代码中的某些内容。我想也许,如果我将绑定调整为仅包含 [AppStatus] 就可以了,但这也不起作用。

错误在两个不同的 FK [StudentID] 和 [JobPostingID] 上引发。

代码:

控制器:

 public ActionResult Edit([Bind(Include = "ApplicationID,StudentID,JobPostingID,Resume,AppStatus")] Application application)
        {
            if (ModelState.IsValid)
            {
                db.Entry(application).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.StudentID = new SelectList(db.Students, "StudentID", "FirstName", application.StudentID);
            ViewBag.JobPostingID = new SelectList(db.JobPostings, "JobPostingID", "Position", application.JobPostingID);
            return View(application);
        }

查看:

<div class="form-horizontal">
    <h4>Application</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.ApplicationID)

    <div class="form-group">
        @Html.LabelFor(model => model.Student.FirstName, "FirstName", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.Student.FirstName)
        </div>
    </div>

 
    <div class="form-group">
        @Html.LabelFor(model => model.JobPosting.Position, "Position", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DisplayFor(model => model.JobPosting.Position)
        </div>
    </div>

  

    <div class="form-group">
        @Html.LabelFor(model => model.AppStatus, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EnumDropDownListFor(model => model.AppStatus, "--Update Application Status--", new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.AppStatus, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

我通常使用这种语法,尤其是当我只想更新某些属性时

public ActionResult Edit( Application application)
        {
if (ModelState.IsValid)
{
  var existedApplication=db.Applications.FirstOrDefault(i=> i.ApplicationID=application.ApplicationID);
     if(existedApplication!=null)
    {
   existedApplication.AppStatus = application.ApplicationStatus;
   db.Entry(existedApplication).Property(i => i.ApplicationStatus).IsModified = true;
   var result = db.SaveChanges();
   }
.....

Serge 的回答几乎解决了我的问题 - 但我确实需要进行一些调整才能使其达到 运行。

  public ActionResult Edit(Application application)
    {
        if (ModelState.IsValid)
        {
            var existedApplication = db.Applications.FirstOrDefault(i => i.ApplicationID == application.ApplicationID);
            if (existedApplication != null)
            {
                existedApplication.AppStatus = application.AppStatus;
                db.Entry(existedApplication).Property(i => i.AppStatus).IsModified = true;
                var result = db.SaveChanges();
                return RedirectToAction("Index");
            }
           
            return View(application);
        }
        return View(application);
    }