I also ran into a similar problem. see my classes below
The author is the parent table
public class Author { [Key] public int AuthorID { get; set; } [Required] public string Name { get; set; } }
Book has AuthorID as foreign key
public class Book { [Key] public int BookId { get; set; } [Required] public string Title { get; set; } public decimal Price { get; set; } public string Genre { get; set; } public DateTime PublishDate { get; set; } public string Description { get; set; } public int AuthorID { get; set; } [ForeignKey("AuthorID")] public virtual Author Author { get; set; } }
I just updated my code and added a virtual below
[ForeignKey("AuthorID")] public virtual Author Author { get; set; }
so basically in the book I have the same property from the author
public int AuthorID { get; set; }
and another virtual property of type Author
[ForeignKey("AuthorID")] public virtual Author Author { get; set; }
Hope this helps.
source share