简体   繁体   English

UPDATE语句与FOREIGN KEY约束冲突

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

Recently I tried to add a one-to-many relationship in my MVC application via Entity Framework Code First. 最近,我尝试通过Entity Framework Code First在我的MVC应用程序中添加一对多关系。 I added the relationship to bind an Administrator name from a dropdown list to the current application that is being filled out. 我添加了关系以将管理员名称从下拉列表绑定到正在填写的当前应用程序。 So I have one table for the administrator names and one for the actual application information. 因此,我有一张表用于管理员名称,另一张表用于实际的应用程序信息。 The application and dropdown list of admin names seem to work fine and all information is going into my database on submit, but when I try to Edit the application, I get the following error: 管理员名称的应用程序和下拉列表似乎运行良好,并且所有信息都在提交时进入我的数据库,但是当我尝试编辑应用程序时,出现以下错误:

The UPDATE statement conflicted with the FOREIGN KEY constraint The conflict occurred in database table "dbo.Administrator", column 'AdministratorId' UPDATE语句与FOREIGN KEY约束冲突在数据库表“ dbo.Administrator”的“ AdministratorId”列中发生了冲突

I've tried setting my Id columns to "Not Null", but this did not solve the issue. 我尝试将Id列设置为“ Not Null”,但这不能解决问题。

Model: 模型:

public class Administrator
{

    public int AdministratorId{ get; set; }
    public string AdministratorName{ get; set; }

}

public class Application
{
    public Application()
    {
        GetDate = DateTime.Now;
    }

    public int ApplicationId { get; set; }

    [DisplayName("Marital Status")]
    public bool? MaritalStatus { get; set; }

    [Required]
    [DisplayName("First Name")]
    public string FirstName { get; set; }

    [DisplayName("Middle Initial")]
    public string MiddleInitial { get; set; }
     [Required]
    [DisplayName("Last Name")]
    public string LastName { get; set; }

    [DisplayName("Date Submitted")]
    public DateTime GetDate { get; set; }

    public int AdministratorId{ get; set; }

    public virtual Administrator Administrator{ get; set; }
}

Controller (Index): 控制器(索引):

 public ActionResult Index()
    {
        ViewBag.LoanOfficerId = new SelectList(db.Administrators, "AdministratorId", "AdministratorName");
        return View();
    }

    // POST: Applications/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Index([Bind(Include = "AdministratorId,FirstName,MiddleInitial,LastName,")] Application application)
    {



        if (ModelState.IsValid)
        {
ViewBag.AdministratorId= new SelectList(db.Administrators, "AdministratorId",     "AdministratorName", application.Administrator);
            db.Applications.Add(application);
            db.SaveChanges();

            return RedirectToAction("Thanks");
        }


        return View(application);
    }

AdministratorId is type int, so will have a default value of 0. Unless you've got an Administrator record in your database with AdministratorId as 0, this will break your foreign key constraint. AdministratorId是int类型,因此默认值为0。除非数据库中的Administrator记录中AdministratorId为0,否则这将破坏外键约束。

Changing the type of AdministratorId to int? 将AdministratorId的类型更改为int? on the Application class should resolve this. 应在Application类上解决此问题。

In my case i had this error when i tried to update an object with null as value for a field which was a foreign key. 就我而言,当我尝试使用空值作为外键字段的值更新对象时,出现了此错误。

So when you update your object either indicate your Fkey or before calling savechanges use : 因此,当您更新对象时,请指出您的Fkey或在调用savechanges之前使用:

db.Entry(Myobject).Property(u => u.MyForeignkey).IsModified = false;
db.SaveChanges();

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

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