Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eager Load many to Many - EF Core [closed]

Hello I have a many to many relationship set up like following.

public class order
{
    public int id { get; set; }    
    public virtual ICollection<OrderProducts> Products { get; set; }
}

public class product
{
    public int id { get; set; }
    public virtual ICollection<OrderProducts> Orders { get; set; }
}

public class OrderProducts
{
    public int OrderId { get; set; }
    public virtual Order Order{ get; set; }

    public int ProductIdId { get; set; }
    public virtual Product Product { get; set; }   
}

I would like to Include (Eager load) all products into their orders, but when using same approach like shown with customer, my product list get populated with OrderProducts-objects and not Product-objects

public IEnumerable<Order> GetAll()
{
    return dataContext.Orders
                      .Include(order => order.Customer)
                      // now include all products aswell..
}

I have tried stuff like witout any luck

  .Include(order => order.Products.Where(op => op.OrderId == order.Id).Select(p => p.Product))

So would appreciate if someone could help me out here.. You'r also most welcome to share any good resources on how to construct more advanced lambdas, since i'm not that familiar with this yet..

like image 830
user3116167 Avatar asked Jan 22 '17 05:01

user3116167


People also ask

What is eager loading in EF core?

Eager loading means that the related data is loaded from the database as part of the initial query. Explicit loading means that the related data is explicitly loaded from the database at a later time.

What does it mean to eagerly load entities on the many side of a one to many relationship?

Advertisements. Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by the use of the Include method. It means that requesting related data be returned along with query results from the database.

How do I stop my EF core from lazy loading?

Disable Lazy Loading To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false.

Does Entity Framework support lazy loading?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading.


1 Answers

Just use ThenInclude() statement:

public class order
{
    public int id { get; set; }    
    public virtual IList<OrderProducts> OrderProducts { get; set; }
}
//...
public IEnumerable<Order> GetAll()
{
    return dataContext.Orders
                      .Include(order => order.OrderProducts)
                      .ThenInclude(orderProducts => orderProducts.Product);
}

As stated here, many-to-many relation is not yet implemented. You have to load your OrderProducts then Select(orderProducts => orderProducts.Product)

Important: IntelliSense completion might tell you can't do this, but you can, just make a real build and it will work

like image 50
Robin Thoni Avatar answered Oct 03 '22 13:10

Robin Thoni