简体   繁体   English

UPDATE 语句与 FOREIGN KEY 约束冲突 MVC Web Application

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

Context:语境:

I want a user to only update one part [AppStatus] of an entry.我希望用户只更新条目的一部分 [AppStatus]。 However, when I test it, I get the error mentioned in the subject.但是,当我测试它时,我得到了主题中提到的错误。 I've seen similar questions asked, and tried doing some of the steps they outlined (using CASCADE for the FK, for instance).我见过类似的问题,并尝试执行他们概述的一些步骤(例如,对 FK 使用 CASCADE)。 But it didn't work.但它没有用。 So it's probably something in the code that I messed up with.所以这可能是我搞砸了代码中的某些内容。 I thought maybe, if I adjust the Bind to only include [AppStatus] that would do it, but that didn't work either.我想也许,如果我将绑定调整为仅包含 [AppStatus] 就可以了,但这也不起作用。

The error throws on two different FKs [StudentID] and [JobPostingID].该错误会引发两个不同的 FK [StudentID] 和 [JobPostingID]。

Code:代码:

Controller: Controller:

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

View:看法:

<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>
}

I usually use this syntax, especially if I want to update only certain properties我通常使用这种语法,尤其是当我只想更新某些属性时

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's answer pretty much fixed my issue- but I did need to make a couple of adjustments to get it to run. 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);
    }

 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM