EF4参照完整性异常首先使用代码?

时间:2012-07-11 19:04:00

标签: asp.net-mvc-3 entity-framework-4.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; }
    }

案例可以包含许多CaseNotes,附件和CaseComplaints。外键是技术人员,客户和EngineModel。

直到今天,一切都很好用Add,并用这个模型编辑。现在,我明白了 “发生了引用完整性约束违规:定义引用约束的属性值在关系中的主体和从属对象之间不一致。” 仅在尝试更新“编辑”视图上的案例记录时。创建和插入很好,因为填写了所有必填字段。 为什么这才刚刚开始发生。仅供参考,如果我检查模型只是因为我调用了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");
    }

1 个答案:

答案 0 :(得分:2)

只是解释一下这个例外的含义:

  

发生了参照完整性约束违规:属性   定义引用约束的值不一致   在关系中的主要和依赖对象之间。

控制器帖子操作中的主要对象为cases.Technicians,从属关系为cases。 “定义引用约束的属性值”是依赖对象中的外键,即cases.TechnicianID,另一方面是主体的主键,即cases.Technicians.TechnicianID

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

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

在任何情况下,您都需要确保cases.TechnicianIDcases.Technicians.TechnicianID具有相同的值,或cases.Techniciansnull。这取决于视图应该执行的操作以及需要更新的数据 - 仅限cases属性或cases.Technicians属性。