Issue with connecting tables in MVC
Edit: it looks like I messed up the inline code function in this post, but you should be able to read it anyway. Sorry.
I am making an application for a book donation service. I am having problems connecting tables with lambda expressions when the foreign key for the left table is contained in the right table (BookAuthor contains foreign keys for BookID and Author ID). The following code is supposed to return a view that contains a table of all books and the associated (possibly multiple) authors.
[HttpPost]
public IActionResult SearchResult(int? DonationID, int? BookID)
{
`List<Donation> donationList =`
database.Donations
.Include(d => d.Book)
.Include(d => d.Book.BookAuthor.Author)
.ToList<Donation>();
return View(donationList);
}
This format gives me errors. A friend told me that I should add a reference to BookAuthor in the Book model like:
public List<BookAuthor> BookAuthorList { get; set; }
and the include statement should look something like:
.Include(d => d.Book.BookAuthorList.Author)
This seems to be the right direction, but I still can't figure out how to make the connections work all the way through to the Author table. I know this is simple, but I am stuck.
0 comments:
Post a Comment