Reputation: 417
I have following tables:
public class Author
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<Book> Books { get; set; } = new List<Book>();
}
public class Book
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
public ICollection<BookBorrower> BookBorrowers { get; set; } = new List<BookBorrower>();
}
public class BookBorrower
{
public int Id { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
public int BorrowerId { get; set; }
public Borrower Borrower { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool IsBookReturned { get; set; } = false;
}
First of all I have one-to-many and many-to-many relationships. Now the question is how to include BookBorrower through the author.
I have those code
var author = await _db.Authors
.Include(a => a.Books)
.FirstOrDefaultAsync(a => a.Id == id);
author contains all of its book, but each book doesn't include its BookBorrowers property.
author.Books.BookBorrowers.Count // that's zero but I have enities in those table for those book.
Upvotes: 1
Views: 44
Reputation: 118937
You need to use ThenInclude
, for example:
var author = await _db.Authors
.Include(a => a.Books)
.ThenInclude(b => b.BookBorrowers) //Add this line
.FirstOrDefaultAsync(a => a.Id == id);
Upvotes: 1