Entity Framework - Invalid Column Name '*_ID"

后端 未结 15 1600
长发绾君心
长发绾君心 2020-11-29 21:30

I\'ve narrowed this down to some issue between Code First and Database first EF, but I\'m not sure how to fix it. I\'ll try to be as clear as I can, but I honestly am missin

15条回答
  •  北海茫月
    2020-11-29 21:50

    This is a late entry for those (like me) who didn't immediately understand the other 2 answers.

    So...

    EF is trying to map to the EXPECTED name from the PARENT TABLES KEY-REFERENCE...and since...the FOREIGN KEY name was "changed or shortened" in the databases CHILD TABLE relationship...you would get the message above.

    (this fix may differ between versions of EF)

    FOR ME THE FIX WAS:
    ADDING the "ForeignKey" attribute to the model

    public partial class Tour
    {
        public Guid Id { get; set; }
    
        public Guid CategoryId { get; set; }
    
        [Required]
        [StringLength(200)]
        public string Name { get; set; }
    
        [StringLength(500)]
        public string Description { get; set; }
    
        [StringLength(50)]
        public string ShortName { get; set; }
    
        [StringLength(500)]
        public string TourUrl { get; set; }
    
        [StringLength(500)]
        public string ThumbnailUrl { get; set; }
    
        public bool IsActive { get; set; }
    
        [Required]
        [StringLength(720)]
        public string UpdatedBy { get; set; }
    
        [ForeignKey("CategoryId")]
        public virtual TourCategory TourCategory { get; set; }
    }
    

提交回复
热议问题