具有不同名称的字段上的外键

时间:2013-04-18 14:49:01

标签: asp.net-mvc

我创建了一个名为tbGeneralOptions的表,它在我的应用程序中保存了一些问题的默认答案。选项分别为是,否,N / A,值分别为1,2,3。

public class tbGeneralOption
{
    public int tbGeneralOptionID { get; set; }

    [Required(ErrorMessage = "Please enter an Option")]
    [Display(Name = "Option")]
    public string OptionName { get; set; }

    public virtual ICollection<tbFamilyMember> tbFamilyMember { get; set; }
    public virtual ICollection<tbFamily> tbFamily { get; set; }

}

在我的tbFamilyMember表中,我将上述选项中的值存储在Disability和PhotoPermission字段中。

public class tbFamilyMember
{
    [Display(Name = "Family Member ID")]
    public int tbFamilyMemberID { get; set; }

    [Display(Name = "Family ID")]
    public int tbFamilyID { get; set; }

    [Required(ErrorMessage = "Please select a Role")]
    [Display(Name = "Role")]
    public int tbFamilyRoleID { get; set; }

    [Display(Name = "Firstname")]
    public string Firstname { get; set; }

    [Display(Name = "Surname")]
    public string Surname { get; set; }

    [Required(ErrorMessage = "Please select if the Family Member has a disability")]
    [Display(Name = "Disability")]
    public int Disability { get; set; }

    [Required(ErrorMessage = "Please select if the Family Member has given permission for photographs")]
    [Display(Name = "Photo Permission")]
    public int PhotoPermission { get; set; }

    public virtual tbFamily tbFamily { get; set; }
    public virtual tbFamilyRole tbFamilyRole { get; set; }
    public virtual tbGeneralOption tbGeneralOption { get; set; }
}

我现在的问题是如何在我的视图中显示存储值的OptionName。

我假设我可以通过外键建立链接但是如果字段名称不匹配可以这样做吗?

1 个答案:

答案 0 :(得分:0)

根据您的评论,您根本不需要tbGeneralOption,因为您的每种类型的选项是固定的和静态的 - 选项不会动态添加。因此,您应该在具有选项的类型上创建选项作为布尔属性。此外,您可以删除这些选项的错误消息,因为根据定义,不需要选项。

我删除了属性tbFamilyIDtbFamilyRoleID,因为您已经在底部引用了family和familyRole,并且可以在那里访问ID,以防您需要显示它们,您可能不应该这样做。这是家庭成员的更新模型:

public class tbFamilyMember
{
    public int tbFamilyMemberID { get; set; }

    [Display(Name = "Firstname")]
    public string Firstname { get; set; }

    [Display(Name = "Surname")]
    public string Surname { get; set; }

    [Display(Name = "Disability")]
    public bool Disability { get; set; }

    [Display(Name = "Photo Permission")]
    public bool PhotoPermission { get; set; }

    public virtual tbFamily tbFamily { get; set; }
    public virtual tbFamilyRole tbFamilyRole { get; set; }
}