我正在與EF代碼第一個庫試圖在預約調度應用程序上工作。EF代碼第一個外鍵的
該模型的我都將是一個客戶端,約會,AppointmentType ...
基本上每一個客戶都能擁有一組約會,每個約會可以有一個AppointmentType ...
的代碼如下:
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; }
}
一切運作良好時,我創建約會類型和客戶端,但是當我創建約會,我得到以下錯誤...
- InnerException {「INSERT語句與FOREIGN KEY約束」Appointment_Client \「發生衝突。衝突發生在數據庫\「Salon.Models.SalonContext \」,表\「dbo.Clients \」,列'ClientID'。\ r \ n語句已被終止。「} System.Exception {System.Data.SqlClient。 SQLEXCEPTION}
如果需要更多的詳細信息,請讓我知道......我只是想弄清楚,如果我缺少的任何設置。
這是當我在帖子上進行調試以創建約會時發生的事情... Al l ID的值爲0,這是正確的,但其他字段不能爲空?或者是否重要......只是不太瞭解事情應該如何看待這是我的第一個EF Code First項目......
一切都是例外。顯示你的數據庫模式,異常說明外鍵約束有衝突。 – 2011-02-28 04:01:52
該屏幕截圖有幫助嗎? – jcreamer898 2011-02-28 04:06:13
當您創建新的約會時,ClientID爲空?可能會引發異常 – 2011-02-28 04:08:04