UPDATE语句与FOREIGN KEY约束冲突 [英] The UPDATE statement conflicted with the FOREIGN KEY constraint

查看:139
本文介绍了UPDATE语句与FOREIGN KEY约束冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我尝试通过Entity Framework Code First在我的MVC应用程序中添加一对多关系.我添加了关系,以将管理员名称从下拉列表绑定到正在填写的当前应用程序.因此,我有一张表用于管理员名称,另一张表用于实际的应用程序信息.管理员名称的应用程序和下拉列表似乎正常运行,并且所有信息都在提交时进入我的数据库,但是当我尝试编辑该应用程序时,出现以下错误:

Recently I tried to add a one-to-many relationship in my MVC application via Entity Framework Code First. 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:

UPDATE语句与FOREIGN KEY约束冲突在数据库表"dbo.Administrator"的'AdministratorId'列中发生了冲突

The UPDATE statement conflicted with the FOREIGN KEY constraint The conflict occurred in database table "dbo.Administrator", column 'AdministratorId'

我尝试将Id列设置为"Not Null",但这不能解决问题.

I've tried setting my Id columns to "Not Null", but this did not solve the issue.

型号:

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

控制器(索引):

 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为int类型,因此默认值为0.除非数据库中的Administrator记录的AdministratorId为0,否则它将中断您的外键约束.

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吗?应在Application类上解决此问题.

Changing the type of AdministratorId to int? on the Application class should resolve this.

这篇关于UPDATE语句与FOREIGN KEY约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆