I am working with the EF Code First library trying to work on an appointment scheduling app.
我正在使用EF代码第一库,尝试开发一个约会调度应用程序。
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}
- InnerException{“插入语句与外键约束冲突”\“appointment ment_client \”。冲突发生在数据库中。SalonContext \”,\“dbo。客户\”,列“ClientID”。声明已被终止。“}系统。异常{ 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...
这就是当我在post上调试创建约会时发生的事情……所有ID都为0,这是正确的,但其他字段是否为空?还是问题……只是不太熟悉事物应该是什么样子这是我的第一个EF代码第一项目…
2 个解决方案
#1
1
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.
根据您的设置,一个约会类型只能有一个约会。这是一对一的映射。在这种情况下,您最好将Appointment类型移动到Appointment实体中。否则,我认为更符合逻辑的是,AppoitmentType可以有多个约会,但一个约会只能有一个约会类型。因此,您应该在您的appointment类型实体中包含一个虚拟的ICollection。
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.
我不确定这就是问题的原因,但它可能是。有时映射错误会导致抛出一些奇怪的异常。试试看,如果你的问题解决了,请告诉我。
#2
0
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
根据您的约束,约定类型和客户端在约定中不能为空。您可以删除约束或在对象属性中设置正确的对象。例如,创建客户端和约会类型,然后为创建的客户端创建约会类型
#1
1
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.
根据您的设置,一个约会类型只能有一个约会。这是一对一的映射。在这种情况下,您最好将Appointment类型移动到Appointment实体中。否则,我认为更符合逻辑的是,AppoitmentType可以有多个约会,但一个约会只能有一个约会类型。因此,您应该在您的appointment类型实体中包含一个虚拟的ICollection。
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.
我不确定这就是问题的原因,但它可能是。有时映射错误会导致抛出一些奇怪的异常。试试看,如果你的问题解决了,请告诉我。
#2
0
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
根据您的约束,约定类型和客户端在约定中不能为空。您可以删除约束或在对象属性中设置正确的对象。例如,创建客户端和约会类型,然后为创建的客户端创建约会类型