EF代码第一外键 [英] EF Code First Foreign Key's

查看:119
本文介绍了EF代码第一外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在与EF Code First图书馆一起努力工作在一个约会安排应用程序。我将会是一个客户端,约会和AppointmentType ...



基本上每个客户端都可以有一组约会,每个约会都可以有一个AppointmentType ...



代码如下:

  public class Client 
{
[ScaffoldColumn false)]
public int ClientID {get;组;

[必需]
public string FirstName {get;组;

[必需]
public string LastName {get;组;

[EmailAddress]
[必需]
public string Email {get;组; }

[DataType(DateTime)]
public DateTime Birthday {get;组;

[必需]
public string CellPhone {get;组; }

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

public virtual ICollection< Appointment>约会{get;组; }

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

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

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

[必需]
public DateTime AppointmentDate {get;组; }
public string Notes {get;组; }

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

[必需]
public string Name {get;组; }
public string描述{get;组; }

public virtual预约约会{get;组; }
}

当我创建约会类型和客户端时,一切都很好,但是当我创建约会时,我收到以下错误...




  • InnerException {INSERT语句与FOREIGN KEY约束冲突\ Appointment_Client\。冲突发生在数据库\Salon.Models.SalonContext\中,表\dbo.Clients\,列ClientID,\\\
    这个声明已被终止。 } System.Exception {System.Data.SqlClient.SqlException}



如果需要更多详细信息,请让我知道...我我只是想弄清楚我是否在设置中缺少任何东西。







这是在我调试后发生的事情来创建约会...所有的ID都是0,这是正确的,但应该其他字段不为空?或者它是重要的...只是不太熟悉如何看起来这是我的第一个EF代码第一个项目...

解决方案

p>根据您的设置,一个AppointmentType只能有一个约会。这是一个一对一的映射。在这种情况下,您最好将AppointmentType移动到预约实体中。否则,我认为更合乎逻辑,AppoitmentType可以有很多约会,但一个约会只能有一个AppointmentType。因此,您的AppointmentType实体中应该有一个虚拟ICollection。

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

[必需]
public string Name {get;组; }
public string描述{get;组; }

public virtual ICollection< Appointment>约会{get;组; }
}

我不知道这是什么导致问题,但可能是。有时映射故障会引起一些奇怪的异常。尝试一下,让我知道你的问题是否得到解决。


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.

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

解决方案

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.

这篇关于EF代码第一外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆