C# 如果是一对一关系,则无法配置该属性
申请人有通知,通知有申请人 我试图在EF中实现此功能,但收到以下错误: “无法将属性'applicationId'配置为导航 属性。该属性必须是有效的实体类型,并且 应该有一个非抽象的getter和setter。用于集合 属性类型必须实现ICollection,其中T是有效的 实体类型。” 不确定,我做错了什么 申请人C# 如果是一对一关系,则无法配置该属性,c#,entity-framework,C#,Entity Framework,申请人有通知,通知有申请人 我试图在EF中实现此功能,但收到以下错误: “无法将属性'applicationId'配置为导航 属性。该属性必须是有效的实体类型,并且 应该有一个非抽象的getter和setter。用于集合 属性类型必须实现ICollection,其中T是有效的 实体类型。” 不确定,我做错了什么 申请人 [Index] [Key] public int ApplicantID { get; set; } public string ApplicantTitle { get; se
[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; }
申请通知
[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; }
错误源于第二个表定义上缺少
ForeignKeyAttribute
注释,即使每个表都有其他表的导航属性。尝试在applicationnotification
上设置外键属性,并将两个表上的导航属性标记为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; }
}
一对一关系的目的是申请人
可以使用或不使用申请通知
进行保存,但是申请通知
要求在保存时(从)存在申请人
类似问题:
我的第一种方法在AppliantID上确实有fk属性,但是这不起作用,重复了相同的错误。然而,奇怪的是,这确实起了作用