简体   繁体   中英

EF Core many-to-many linq query

If I have the following classes mapping a many-to-many relationship in EF Core:

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  
public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<BookCategory> BookCategories { get; set; }
}  
public class BookCategory
{
    public int BookId { get; set; }
    public Book Book { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

what is be the best practice to get a List<Category> from the Book instance? In EF6 I was able to just Query Book.Categories

If you're using EF Core 5.0 or later, you no longer need the BookCategory entity. You can simply have ICollection<Category> / ICollection<Book> , as you would have done with Entity Framework 6.

public class Book
{
    public int BookId { get; set; }
    public string Title { get; set; }
    public Author Author { get; set; }
    public ICollection<Category> Categories { get; set; }
}  
public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public ICollection<Book> Books { get; set; }
}  

For EF Core 3.1 or earlier - which is the last version to support .NET Framework, so you may be stuck with it - you will need to include both levels of navigation properties, and then select the list of categories from that:

Book book = await context.Books
    .Include(b => b.BookCategories).ThenInclude(c => c.Category)
    .First(b => b.Id == 42);

List<Category> bookCategories = book.Categories
    .Select(c => c.Category)
    .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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