2017-06-12 89 views
2

我試圖更新ApplicantApplicantNotification OneToOne關係表在數據庫中。但是,我正在嘗試不起作用。EF更新兩個實體不在數據庫中更新

我通過EF選擇對象然後覆蓋屬性,所以我會假設EF將跟蹤更改但不反映回數據庫。

問題: 如何更新EF中的兩個實體?

申請人:

[Index] 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int ApplicantID { get; set; } 
    [Required] 
    public string ApplicantTitle { get; set; } 
    [Required] 
    public string Firstname { get; set; } 
    [Required] 
    public string Lastname { get; set; } 
    [Required] 
    public string Address { get; set; } 
    [Required] 
    public string Address1 { get; set; } 
    [Required] 
    public string Address2 { get; set; } 
    [Required] 
    public string Address3 { get; set; } 
    [Required] 
    public string Postcode { get; set; } 
    [Required] 
    public string CaseReference { get; set; } 
    [DataType(DataType.Date)] 
    public DateTime DateOfBirth { get; set; } 
    public ApplicantNotification Notification { get; set; } 

ApplicantNotification:

[Index] 
     [Key, Column("ApplicantID"), ForeignKey("Applicant")] 
     public int ApplicantNotificationID { get; set; } 
     public bool FirstNotification { get; set; } 
     public bool SecondtNotification { get; set; } 
     public bool ThirdNotification { get; set; } 
     public bool FinalNotification { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime? ReminderDate { get; set; } 
     public int ReminderFrequency { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime? FirstNotificationDate { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime? SecondNotificationDate { get; set; } 
     [DataType(DataType.Date)] 
     public DateTime? ThirdNotificationDate { get; set; } 
     public bool IsArchive { get; set; } 
     public virtual Applicant Applicant { get; set; } 

方法:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Edit(ApplicantNotificationViewModel model) 
{ 
    if (ModelState.IsValid) 
    { 
     //select from ef 
     var applicant = (from a in db.Applicants 
         where a.ApplicantID == model.ApplicantID 
         select a).FirstOrDefault(); 

     var applicantNotification = (from an in db.ApplicantNotifcations 
            where an.ApplicantNotificationID == model.ApplicantID 
            select an).FirstOrDefault(); 

     SetApplicant(model, applicant); 
     SetApplicantNotification(model, applicantNotification); 

     using (var context = new WorkSmartContext()) 
     { 
       try 
       { 
        db.Entry(applicant).State = EntityState.Modified; 

        db.Entry(applicantNotification).State = EntityState.Modified; 
        context.SaveChanges(); 

       } 
       catch (Exception ex) 
       { 

       } 

     } 
     return RedirectToAction("Index"); 
    } 
    return View(model); 
} 

感謝

+0

不,我也試過,也不更新表 – Haris

+1

爲什麼在這種情況下的交易?英孚已經將其包裝在一個。請參閱[這裏](https://msdn.microsoft.com/en-us/library/dn456843(v = vs.113).aspx) –

+0

因爲我正在更新兩個實體?假設它會拋出第二個SaveChanges()的異常?這兩個表之間還存在OneToOne關係 – Haris

回答

0

你看起來要試圖穿越dbContexts什麼的,機智豪特見狀WorkSmartContext定義很難拼湊但你可能想要的是更多這樣的:

var applicant = db.Applicants 
    .Include(a=>a.Notification) 
    .FirstOrDefault(a=>a.ApplicantId = model.ApplicantId); 

if (applicant == null) 
    // Handle missing Applicant scenario here. 

SetApplicant(model, applicant); 
SetNotification(model, applicant.Notification); 

db.SaveChanges(); 

你的申請人到相關通知的參考。用它。你不需要加載個人相關實體。您可以執行諸如檢查現有相關實體,更新它們,在必要時插入等等。在需要更新不相關實體但確保全部成功或失敗的情況下,請使用事務或工作單元模式。 (我猜這個WorkSmartContext應該是,但沒有派生或綁在「分貝」,我不明白這是怎麼回事)。

+0

真棒,想知道如何使用導航屬性來做到這一點 – Haris