繁体   English   中英

EF更新数据库启用迁移导航属性错误

[英]EF Update DB Enable Migration Navigational Property Error

我正在将EF Migrations与MVC 5结合使用-但是,我得到了我认为/认为是的/是一个非常常见的错误,但是我所看到的常见解决方案已经:

属性'companyId'不能配置为导航属性。该属性必须是有效的实体类型,并且该属性应具有非抽象的getter和setter。 对于集合属性,该类型必须实现ICollection,其中T是有效的实体类型。

通过环顾四周,我的代码看起来是正确的,并且在启用迁移更新数据库之前进行了工作,以查看在我将该更改付诸实施之前,系统中的测试数据是否已被擦除。 在ASN模型中,我添加了一个testAsn的字符串属性,以查看是否将此属性作为列添加到数据库中。

我不确定此错误是否与ASN或联系人表/模型有关,但是它们都具有相同的实现。

我是否误解了导航属性的使用? 任何帮助将不胜感激。

楷模

[Table("Company")]

    public class Company
    {
        [Key]
        public int companyId { get; set; }
        [Required(ErrorMessage="Company name required.")]
        [StringLength(100)]
        public string name { get; set; }
        [Required(ErrorMessage = "Telephone required.")]
        public string telephone { get; set; }
        [Required(ErrorMessage="Registration Number required.")]
        [StringLength(30)]
        public string regNumber { get; set; }
        // navigation properties  of the models that belong to the company
        public virtual IList<Asn> asns { get; set; }
        public virtual IList<Contact> contacts { get; set; }
    }
[Table("Asn")]
    public class Asn
    {
        [Key]
        public int asnId { get; set; }
        public int companyId { get; set; }
        [ForeignKey("companyId")]
        // property I'm adding to test whether migrations is working
        public string testAsn { get; set; }
        // *******************************
        // as pointed out in the correct answer, the property below
        // this comment should be above testAsn
        // *******************************
        public virtual Company company { get; set; }
        [Required] // always has value
        public bool userBehalf { get; set; }
        [Required] // always has value
        public bool confirmAssignment { get; set; }
        [Required(ErrorMessage="Prefix required.")]
        public string prefixAnnounced { get; set; }
        [Required(ErrorMessage="Pending Ticket ID required.")]
        public string pendingTicket { get; set; }
        [DataType(DataType.MultilineText)]
        public string comments { get; set; }
        public bool asNumType { get; set; }
        public string reason { get; set; }
    }
    [Table("Contact")]
    public class Contact
    {
        [Key]
        public int contactId { get; set; }
        public int companyId { get; set; }
        [ForeignKey("companyId")]
        public virtual Company company { get; set; }
        [StringLength(100, MinimumLength=3)]
        [Required(ErrorMessage="Contact name required.")]
        public string name { get; set; }
        [Required(ErrorMessage="Telephone required.")]
        [StringLength(30, MinimumLength=11)]
        public string telephone { get; set; }
        [Required]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                            ErrorMessage = "Email is not valid.")]
        public string email { get; set; }
        [Required(ErrorMessage = "Contact type required.")]
        public string type { get; set; }
    }    

您在Asn类中放错了ForeignKey属性。 而不是将其放在company属性上,而是在testAsn属性上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM