asp.net-mvc – MVC 4 Code First属性上的ForeignKeyAttribute …类型…无效

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC 4 Code First属性上的ForeignKeyAttribute …类型…无效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直收到这个错误,我不知道为什么.

类型为’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; }
}

谁能帮我解决这个问题?

解决方法

[ForeignKey(“WardId”)]表示用作Ward表的外键的属性应该是BioSheetModel类的WardId属性.

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

public int WardId {get; set;}

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

public int? WardId {get; set;}

为可空/可选关系.

原文链接:https://www.f2er.com/aspnet/251078.html

猜你在找的asp.Net相关文章