2017-06-06 2 views
0

申請者に通知があり、通知に申請者があります。 EFの1対1の関係、プロパティを設定することができません

私は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; } 

誤差が第2のテーブル定義にForeignKeyAttribute注釈が存在しないことに由来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

も各テーブルは、他のテーブルのナビゲーションプロパティを有します。 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; } 
} 

1対1の関係の目的はApplicantがでたりApplicantNotificationもしなくても保存することができますですが、ApplicantNotificationApplicantが必要です(tutorialから)それを保存するときに存在する必要があります。

同様の問題:

Entity framework Code First One-to-One relationship

+0

私の最初のアプローチは機能しなかったことが、ApplicantIDのFK属性を持っていたし、同じエラーを繰り返しました。これはしかし、トリック、奇妙な – Haris

関連する問題