渴望加载多对多 - EF核心 [英] Eager Load many to Many - EF Core

查看:121
本文介绍了渴望加载多对多 - EF核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有很多关系设置如下。

  public class order 
{
public int id {get;组; }
public virtual ICollection< OrderProducts>产品{get;组; }
}

public class product
{
public int id {get;组; }
public virtual ICollection< OrderProducts>订单{get;组; }
}

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

public int ProductIdId {get;组; }
public virtual Product Product {get;组;我想把所有产品都包含(Eager)加载到他们的订单中,但是当使用与客户所示的相同方法时,我的产品列表中填充有OrderProducts对象而不是Product-objects。

  public IEnumerable< ;排序> GetAll()
{
return dataContext.Orders
.Include(order => order.Customer)
//现在包括所有产品as .. ..
}

我尝试过任何运气的东西

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

所以不妨感谢如果有人可以帮助我在这里..
你也最欢迎分享关于如何构建更高级的羊羔的任何好的资源,因为我不熟悉这一点..

解决方案

只是使用 ThenInclude()语句:

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

正如这里,多对多关系尚未实现。您必须加载您的 OrderProducts 然后选择(orderProducts => orderProducts.Product)


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..

解决方案

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)

这篇关于渴望加载多对多 - EF核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆