属性' FontNameId'上的ForeignKeyAttribute on type' ConferenceManagementSystem.Models.FontName'无效

时间:2015-08-21 14:30:01

标签: asp.net-mvc-4

这里的新网站,我尝试了几种解决类似问题但无济于事的解决方案,所以非常感谢您的帮助。

错误:

  

属性' FontNameId'上的ForeignKeyAttribute on type' ConferenceManagementSystem.Models.FontName'无效。导航属性' AbstractFileFormat'未在依赖类型' ConferenceManagementSystem.Models.FontName'上找到。 Name值应该是有效的导航属性名称。

代码段:

namespace ConferenceManagementSystem.Models
{
    [Table("AbstractFileFormat")]
    public class AbstractFileFormat
    {
        [Key]
        public int AbstractFileFormatId { get; set; }

        [Required]
        public string Title_HorizontalAlignment { get; set; }

        [Required]
        [ForeignKey("FontNameId")]
        public int Title_FontName { get; set; }

        [Required]
        public string Title_FontSize { get; set; }

        [Required]
        public bool Title_Bold { get; set; }

        [Required]
        public bool Title_Italic{ get; set; }

        [Required]
        public string Name_HorizontalAlignment { get; set; }

        [Required]
        [ForeignKey("FontNameId")]
        public int Name_FontName { get; set; }

        [Required]
        public string Name_FontSize { get; set; }

        [Required]
        public bool Name_Bold { get; set; }

        [Required]
        public bool Name_Italic { get; set; }

        [Required]
        public string Address_HorizontalAlignment { get; set; }

        [Required]
        [ForeignKey("FontNameId")]
        public int Address_FontName { get; set; }

        [Required]
        public string Address_FontSize { get; set; }

        [Required]
        public bool Address_Bold { get; set; }

        [Required]
        public bool Address_Italic { get; set; }

        [Required]
        public string Email_HorizontalAlignment { get; set; }

        [Required]
        [ForeignKey("FontNameId")]
        public int Email_FontName { get; set; }

        [Required]
        public string Email_FontSize { get; set; }

        [Required]
        public bool Email_Bold { get; set; }

        [Required]
        public bool Email_Italic { get; set; }

        public float LineSpacing { get; set; }

        public int ConferenceId { get; set; }

        public virtual Conference Conference { get; set; }

        public virtual FontName FontName { get; set; }

    }
}

第二个片段:

namespace ConferenceManagementSystem.Models
{
    [Table("FontName")]
    public class FontName
    {
        [Key]
        public int FontNameId { get; set; }

        public int Name { get; set; }

        public virtual ICollection<AbstractFileFormat> AbstractFileFormats { get; set; }

    }
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我做了这个,但它确实有效。

AbstractFileFormat类中添加另一个属性,该属性对应于AbstractFileFormat表中的外键。即指向FontNameId表中FontName字段的字段。像这样:

public int FontNameId { get; set; }

[Required]
[ForeignKey("FontNameId")]
public int Email_FontName { get; set; }