EF Code First Foreign Key's

2019-07-23 05:03发布

I am working with the EF Code First library trying to work on an appointment scheduling app.

The model's I have are going to be a Client, Appointment, and AppointmentType...

Basically each Client can have a set of Appointments, and each Appointment can have a single AppointmentType...

The code is as follows:

public class Client
 {
      [ScaffoldColumn(false)]
      public int ClientID { get; set; }

      [Required]
      public string FirstName { get; set; }

      [Required]
      public string LastName { get; set; }

      [EmailAddress]
      [Required]
      public string Email { get; set; }

      [DataType("DateTime")]
      public DateTime Birthday { get; set; }

      [Required]
      public string CellPhone { get; set; }

      public string HomePhone { get; set; }
      public string Notes { get; set; }

      public virtual ICollection<Appointment> Appointments{ get; set; }

      public string Name { 
           get{
                return FirstName + " " + LastName;
           }
      }
public class Appointment
 {
      [ScaffoldColumn(false)]
      public int AppointmentID { get; set; }

      [ScaffoldColumn(false)]
      public int ClientID { get; set; }

      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public DateTime AppointmentDate { get; set; }
      public string Notes { get; set; }

      public virtual AppointmentType AppointmentType { get; set; }
      public virtual Client Client { get; set; }
 }
     public class AppointmentType
 {
      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public string Name { get; set; }
      public string Description { get; set; }

      public virtual Appointment Appointment { get; set; }
 }

Everything works well when I create an appointment type, and a client, but when I create an appointment I get the following error...

  • InnerException {"The INSERT statement conflicted with the FOREIGN KEY constraint \"Appointment_Client\". The conflict occurred in database \"Salon.Models.SalonContext\", table \"dbo.Clients\", column 'ClientID'.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}

If more details are needed, please let me know...I am just trying to figure out if I am missing anything in the setup.

enter image description here

enter image description here

This is what happens when I debug on the post to create the Appointment...All the ID's are as 0 which is correct, but should the other fields not be null?? Or does it matter...Just not very familiar with how things should look this being my first EF Code First project...

2条回答
Animai°情兽
2楼-- · 2019-07-23 05:57

By your constraints AppointmentType and Client cannot be null in Appointment. You can delete constraints or set correct objects in object properties. For example create Client and AppointmentType and then create Appointment for created Client with created AppointmentType

查看更多
仙女界的扛把子
3楼-- · 2019-07-23 06:01

According to your setup, one AppointmentType can only have one Appointment. This is a one-to-one mapping. In this case, you better move the AppointmentType into the Appointment entity. Otherwise, what I believe is more logical, an AppoitmentType can have many Appointments but one Appointment can have only one AppointmentType. Accordingly, you should have a virtual ICollection inside your AppointmentType entity.

 public class AppointmentType
 {
      [ScaffoldColumn(false)]
      public int AppointmentTypeID { get; set; }

      [Required]
      public string Name { get; set; }
      public string Description { get; set; }

      public virtual ICollection<Appointment> Appointments { get; set; }
 }

I am not sure this is what's causing the problem but it could be. Sometimes mapping faults cause some weird exceptions to be thrown. Give it a try and let me know if your problem gets resolved.

查看更多
登录 后发表回答