1

我有一个非常简单的模型,如下所示:

public class Cases
    {
    //case data model for call center
    //implement lists for all related child tables too

    [Key]
    public int CasesID { get; set; }

    public string CaseNumber { get; set; }

    [Required(ErrorMessage = "Customer is Required")]
    public int CustomerID { get; set; }
    public Customer Customer { get; set; }

    [MaxLength(50)]
    public string UserName { get; set; }  //get user name from the aspnet membership

    [Required(ErrorMessage = "Case Category is Required")]
    public int CaseCategoryID { get; set; }

    [Required(ErrorMessage = "Technician is Required")]
    public int TechnicianID { get; set; }
    public Technician Technicians { get; set; }

    [Required(ErrorMessage = "Engine Model is Required")]
    public int EngineModelID { get; set; }
    public EngineModel EngineModel { get; set; }

    [MaxLength(50)]
    public string BMSWorkorder { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Status is Required")]
    public string CaseStatus { get; set; }

    [MaxLength(50)]
    public string OpenedBy { get; set; }

    [Required(ErrorMessage = "Opened Date is Required")]
    [DataType(DataType.DateTime)]
    public DateTime? OpenedDate { get; set; }

    [MaxLength(50)]
    public string ClosedBy { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime? ClosedDate { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage="Caller First Name is Required")]
    public string CallerFirstName { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Caller Last Name is Required")]
    public string CallerLastName { get; set; }

    [MaxLength(10)]
    [Required(ErrorMessage = "Qualified is Required")]
    public string Qualified { get; set; }

    public string Description { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "ESN is Required")]
    public string ESN { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "Mileage is Required")]
    public string Mileage { get; set; }

    [DataType(DataType.Date)]
    public DateTime? DateInService { get; set; }

    [MaxLength(50)]
    public string ESTR { get; set; }

    [MaxLength(50)]
    [Required(ErrorMessage = "EDS is Required")]
    public string EDS { get; set; }

    [MaxLength(50)]
    public string GensetSerialNumber { get; set; }

    [MaxLength(50)]
    public string GensetModelNumber { get; set; }

    //child Case Notes records
    public virtual ICollection<CaseNotes> CaseNotes { get; set; }

    //child case attachment records
    public virtual ICollection<Attachment> Attachments { get; set; }

    //child case complaint records
    public virtual ICollection<CaseComplaint> CaseComplaint { get; set; }

    //tracking fields
    public DateTime? CreatedOn { get; set; }
    [MaxLength(50)]
    public string CreatedBy { get; set; }
    public DateTime? ModifiedOn { get; set; }
    [MaxLength(50)]
    public string ModifiedBy { get; set; }
    }

一个案例可以有许多案例注释、附件和案例投诉。外键是 Technicians、Customers 和 EngineModel。

直到今天,使用此模型的 Add 和 Edits 一切正常。现在,我出乎意料地得到“发生参照完整性约束违规:定义参照约束的属性值在关系中的主体和依赖对象之间不一致。 ”仅在尝试更新编辑视图上的案例记录时。创建和插入很好,因为所有必填字段都已填写。为什么这会开始发生。仅供参考,如果我检查模型只是为了调用 context.SaveChanges,我注意到所有数据看起来都正确,FK 值已填充,但是外键类(技术人员)的导航属性中的数据几乎看起来像认为我正在尝试在这些类中插入新记录...

这是我的控制器编辑操作:

public ActionResult Edit(int id)
    {
        Cases cases = db.Cases.Find(id);
        db.Entry(cases).Reference(x => x.Customer).Load();
        db.Entry(cases).Collection(x => x.CaseComplaint).Load();
        db.Entry(cases).Collection(x => x.CaseNotes).Load();
        db.Entry(cases).Reference(x => x.Technicians).Load();

        GetCaseCategoryLookup(cases.CaseCategoryID);
        GetEngineModelLookup(cases.EngineModelID);
        GetTechnicianLookup(cases.TechnicianID);
        GetQualifiedList(cases.Qualified);
        GetCaseStatusList(cases.CaseStatus);

        return View(cases);
    }

    //
    // POST: /Cases/Edit/5

    [HttpPost]
    public ActionResult Edit(Cases cases)
    {
        if (ModelState.IsValid)
        {
        AppHelpers help = new AppHelpers();

            if (cases.CaseStatus == "CLOSED")
                {
                cases.ClosedBy = "USER";
                cases.ClosedDate = help.GetEasternTime();
                }
            cases.ModifiedBy = "USER";
            cases.ModifiedOn = help.GetEasternTime();

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

        }

        return RedirectToAction("Index");
    }
4

1 回答 1

2

只是为了解释异常的含义:

发生参照完整性约束冲突:定义参照约束的属性值在关系中的主体对象和从属对象之间不一致。

您的控制器发布操作中的主体对象是cases.Technicians,依赖对象是cases。“定义引用约束的属性值”是依赖对象中的外键,即cases.TechnicianID,另一方面是主体的主键,即cases.Technicians.TechnicianID

例外情况是这两个值是不同的:cases.TechnicianID != cases.Technicians.TechnicianID但是当您更新实体时它们必须相同。

我不知道你的观点到底应该做什么。您只能编辑cases实体或cases.Technicians属性吗?还是只能指派一名新的现有技术人员cases?如果是后者,我很奇怪为什么模型绑定器会创建一个cases.Technicians实例,因为不应该有任何表单字段包含cases.Technicians. cases.TechnicianID要分配另一个现有技术人员,将外键绑定到表单字段就足够了。

在任何情况下,您都需要确保 anycases.TechnicianIDcases.Technicians.TechnicianID具有相同的值或者 that cases.Techniciansis null。这取决于视图应该做什么以及需要更新哪些数据——也只有cases属性或cases.Technicians属性。

于 2012-07-12T11:32:08.060 回答