我一直收到这个错误,我不知道为什么。
类型为'BioSheet.Models.BioSheetModel'的属性'Ward'上的ForeignKeyAttribute无效。在依赖类型“BioSheet.Models.BioSheetModel”上找不到外键名称“WardId”。 Name值应该是以逗号分隔的外键属性名称列表。
public class Ward
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("AddressId")]
[Required]
public virtual Address WardAddress { get; set; }
[ForeignKey("BioSheetId")]
public virtual List<BioSheetModel> BioSheets { get; set; }
[Required]
public String Code { get; set; }
}
public class BioSheetModel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
public String FirstName { get; set; }
[Required]
public String LastName { get; set; }
public String Email { get; set; }
[ForeignKey("WardId")]
[Required]
public Ward Ward { get; set; }
public String CellPhoneNumber { get; set; }
public String HouseNumber { get; set; }
[Required]
public String DoB { get; set; }
[Required]
public Address Address { get; set; }
public String OtherInformation { get; set; }
public String PreviousCallings { get; set; }
[ForeignKey("TimePeriodId")]
public virtual TimePeriod TimePeriods { get; set; }
public String HomeWard { get; set; }
public Boolean OkToText { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
public DateTime TodaysDate { get; set; }
[ForeignKey("EMPId")]
public virtual EDUEMP EduEmp { get; set; }
[ForeignKey("SingId")]
public virtual Sing Singing { get; set; }
[ForeignKey("MissionId")]
public virtual Mission MissionIn { get; set; }
}
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:11)
[ForeignKey("WardId")]
表示用作Ward表的外键的属性应该是WardId
类的BioSheetModel
属性。
您收到错误是因为您尚未在WardId
类上定义BioSheetModel
属性。
添加
public int WardId {get; set;}
表示不可为空/需要的关系,或
public int? WardId {get; set;}
为可空/可选关系。