2017-06-06 42 views
0

申請人有通知,通知有申請人。EF一對一關係,無法配置房產

我想在EF來實現這一點,但我收到以下錯誤:

"The property 'ApplicantID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type."

不知道,我在做什麼錯。

申請人

[Index] 
[Key] 
public int ApplicantID { get; set; } 
public string ApplicantTitle { get; set; } 
public string Firstname { get; set; } 
public string Lastname { get; set; } 
public string Address { get; set; } 
public string Address1 { get; set; } 
public string Address2 { get; set; } 
public string Address3 { get; set; } 
public string Postcode { get; set; } 
//fk 
public int ApplicantNotificationID { get; set; } 
public ApplicantNotification Notification { get; set; } 

ApplicantNotification

 [Index] 
     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; } 
     //FK 
     public int ApplicantID { get; set; } 

     //navigation property 
     public Applicant Applicant { get; set; } 

回答

0

從缺席ForeignKeyAttribute註釋在第二表定義的起源的誤差,即使每個表具有用於其它表導航屬性。嘗試設置外鍵屬性上ApplicantNotification和標記兩個表導航財產virtual這樣的:

[Table("Applicant")] 
public class Applicant 
{ 
    [Index] 
    [Key] 
    public int ApplicantID { get; set; } 

    // other properties 

    public virtual ApplicantNotification Notification { get; set; } 
} 


[Table("ApplicantNotification")] 
public class ApplicantNotification 
{ 
    [Index] 
    [Key, ForeignKey("Applicant")] 
    public int ApplicantNotificationID { get; set; } 

    // other properties 

    public virtual Applicant Applicant { get; set; } 
} 

的一個一對一關係的目的是Applicant可以使用或不保存ApplicantNotificationApplicantNotification需要Applicant在保存時存在(從tutorial)。

類似的問題:

Entity framework Code First One-to-One relationship

+0

我的第一種方法確實對ApplicantID的FK屬性,但沒有工作,並重復同樣的錯誤。然而這是個詭計,很奇怪 – Haris