2017-06-09 51 views
0

我試圖在數據庫中插入一條新記錄,沒有錯誤,申請人和ApplicantNotification表中沒有創建新記錄。不知道我在做什麼錯了?無法一對一地保存實體EF mvc 5

申請人

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

    /*Spouse*/ 
    public string SpouseTitle { get; set; } 
    public string SpouseFirstname { get; set; } 
    public string SpouseLastname { get; set; } 
    public string SpouseAddress { get; set; } 
    public string SpouseAddress1 { get; set; } 
    public string SpouseAddress2 { get; set; } 
    public string SpouseAddress3 { get; set; } 
    public string SpousePostcode { 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; } 
     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; } 

視圖模型

 public int ApplicantID { get; set; } 
     [Required] 
     public string ApplicantTitle { get; set; } 
     public string ApplicantFirstname { get; set; } 
     public string ApplicantLastname { get; set; } 
     public string ApplicantAddress { get; set; } 
     public string ApplicantAddress1 { get; set; } 
     public string ApplicantAddress2 { get; set; } 
     public string ApplicantAddress3 { get; set; } 
     public string ApplicantPostcode { get; set; } 
     [Required] 
     public string ApplicantCaseReference { get; set; } 
     [Required] 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     public DateTime ApplicantDateOfBirth { get; set; } 
     /*Spouse*/ 
     public string SpouseTitle { get; set; } 
     public string SpouseFirstname { get; set; } 
     public string SpouseLastname { get; set; } 
     public string SpouseAddress { get; set; } 
     public string SpouseAddress1 { get; set; } 
     public string SpouseAddress2 { get; set; } 
     public string SpouseAddress3 { get; set; } 
     public string SpousePostcode { get; set; } 
     /*Notification*/ 
     public int ApplicantNotificationID { get; set; } 
     public bool FirstNotification { get; set; } 
     public bool SecondNotification { get; set; } 
     public bool ThirdNotification { get; set; } 
     public bool FinalNotification { get; set; } 
     public DateTime? ReminderDate { get; set; } 

科瑞e方法:

// POST: Applicant/Create 
[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(ApplicantNotificationViewModel model) 
{ 
    var applicant = new Applicant(); 
    var applicantNotification = new ApplicantNotification(); 

     if (ModelState.IsValid) 
     { 
      SetApplicant(model, applicant); 
      SetApplicantNotification(model, applicantNotification); 

      using (var context = new WorkSmartContext()) 
      { 
       using (var dbContextTransaction = context.Database.BeginTransaction()) 
       { 
        try 
        { 
         db.Applicants.Add(applicant); 
         context.SaveChanges(); 
         db.ApplicantNotifcations.Add(applicantNotification); 
         context.SaveChanges(); 
         dbContextTransaction.Commit(); 
        } 
        catch (Exception) 
        { 
         dbContextTransaction.Rollback(); 
        } 
       } 

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

你爲什麼檢查model.isValid兩次 – hasan

+1

沒有關係,但是你使用了一個視圖模型。刪除那個糟糕的'[Bind]'屬性 –

+0

@hasan對不起,這是一個排字錯誤 – Haris

回答

0

感謝您對評論中的建議。

看來,如果datetime列設置爲允許null,那麼datetime必須設置爲null或設置爲正確的格式才能使sql datetime工作。否則,它拋出

"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\The statement has been terminated." 

https://stackoverflow.com/questions/4608734/the-conversion-of-a-datetime2-data-type-to-a-datetime-data-type-resulted-in-an-o

我設定的日期以空在我的實體對象,並進入一個新的條目到數據庫中。

相關問題