2017-06-09 3 views
0

Ich versuche, einen neuen Datensatz in die Datenbank einzufügen, keine Fehler, ein neuer Datensatz wird nicht in Applicant und ApplicantNotification-Tabelle erstellt. Nicht sicher, was ich falsch mache?Kann Entität eins zu eins nicht speichern EF mvc 5

Anmelderin

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

Ansichtsmodell

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

erstellen e Methode:

// 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

Warum überprüfen Sie model.isValid zweimal – hasan

+1

Keine verwandten, aber Ihre Verwendung eines Ansichtsmodells. Löschen Sie dieses schreckliche '[Bind]' Attribut –

+0

@hasan Entschuldigung, dass ein Tippfehler war – Haris

Antwort

0

Danke für die Vorschläge in den Kommentaren.

Es erscheint, wenn die datetime-Spalte auf null gesetzt ist, dann muss datetime entweder auf null gesetzt oder auf das richtige Format gesetzt werden, damit sql datetime funktioniert. Ansonsten wirft es die

"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

ich die Daten auf null in meinem Entity-Objekte festgelegt und trat einen neuen Eintrag in die Datenbank.

Verwandte Themen