5

我不断收到此错误,我不知道为什么。

“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; }
}

谁能帮我解决这个问题?

4

1 回答 1

13

[ForeignKey("WardId")]指示用作 Ward 表的外键的属性应该是WardId类上的属性BioSheetModel

您收到错误是因为您尚未在该类上定义WardId属性。BioSheetModel

添加

public int WardId {get; set;}

对于不可为空/必需的关系,或

public int? WardId {get; set;}

对于可空/可选关系。

于 2013-09-19T00:48:54.820 回答