我有一个非常简单的模型,如下所示:
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");
}