MVC 4 code首先ForeignKeyAttribute财产上的... ...类型无效 [英] MVC 4 Code First ForeignKeyAttribute on property ... on type ... is not valid

查看:935
本文介绍了MVC 4 code首先ForeignKeyAttribute财产上的... ...类型无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I keep getting this error and I do not know why.

该ForeignKeyAttribute财产沃德的类型BioSheet.Models.BioSheetModel'无效。外键名'WardId不是在依赖型BioSheet.Models.BioSheetModel'找​​到。名称值应为外键的属性名称的逗号分隔的列表。

The ForeignKeyAttribute on property 'Ward' on type 'BioSheet.Models.BioSheetModel' is not valid. The foreign key name 'WardId' was not found on the dependent type 'BioSheet.Models.BioSheetModel'. The Name value should be a comma separated list of foreign key property names.

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

谁能帮我解决这个问题?

Can anyone help me resolve this?

推荐答案

[ForeignKey的(WardId)] 表示属性作为一个外键使用沃德表应该是在 BioSheetModel 类的<​​code> WardId 属性。

[ForeignKey("WardId")] indicates that the property to use as a foreign key to the Ward table should be the WardId property on the BioSheetModel class.

您会得到错误,因为你还没有定义的 BioSheetModel WardId 属性。

You're getting the error because you haven't defined a WardId property on the BioSheetModel class.

添加

public int WardId {get; set;}

对于非空的/所需的关系,或

for a non-nullable/required relationship, or

public int? WardId {get; set;}

可空/可选的关系。

for a nullable/optional relationship.

这篇关于MVC 4 code首先ForeignKeyAttribute财产上的... ...类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆