Entity framework 快速加载导航属性

Entity framework 快速加载导航属性,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,在我的存储库中,我正在尝试使用渴望加载来加载相关的实体。不知道为什么,但当我返回特定实体的所有实例时,似乎会返回相关实体,但当我限制返回的结果时,相关实体不会包含在结果中 服务层中的此代码将返回所有订单,包括相关客户、OrderItem和产品实体: public async Task<IEnumerable<Order>> GetOrdersAsync() { return await _repository.GetAsync(null, q => q.Or

在我的存储库中,我正在尝试使用渴望加载来加载相关的实体。不知道为什么,但当我返回特定实体的所有实例时,似乎会返回相关实体,但当我限制返回的结果时,相关实体不会包含在结果中

服务层中的此代码将返回所有订单,包括相关客户、OrderItem和产品实体:

public async Task<IEnumerable<Order>> GetOrdersAsync()
{
    return await _repository.GetAsync(null, q => q.OrderByDescending(p => p.CreatedDate), "Customer", "OrderItems", "OrderItems.Product");
}
public async Task<Order> GetOrderByIdAsync(long id)
{
  return await _repository.GetByIdAsync(id, "Customer", "OrderItems", "OrderItems.Product");
}
public异步任务GetOrdersAsync()
{
return wait_repository.GetAsync(null,q=>q.OrderByDescending(p=>p.CreatedDate),“Customer”、“OrderItems”、“OrderItems.Product”);
}
在存储库中:

public async Task<IEnumerable<Order>> GetAsync(Expression<Func<Order, bool>> where = null, Func<IQueryable<Order>, IOrderedQueryable<Order>> orderBy = null, params string[] navigationProperties)
{
    IQueryable<Order> query = _context.Set<Order>();

    if (where != null)
    {
        query = query.Where(where);
    }

    //Apply eager loading
    foreach (string navigationProperty in navigationProperties)
            query = query.Include(navigationProperty);

    if (orderBy != null)
    {
        return await orderBy(query).ToListAsync();
    }
    else
    {
        return await query.ToListAsync();
    }
}
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
    DbSet<Order> dbSet = _context.Set<Order>();

    foreach (string navigationProperty in navigationProperties)
        dbSet.Include(navigationProperty);

    return await dbSet.FindAsync(id);
}
public async Task GetAsync(表达式where=null,Func orderBy=null,参数string[]navigationProperties)
{
IQueryable查询=_context.Set();
if(其中!=null)
{
query=query.Where(Where);
}
//应用即时加载
foreach(navigationProperties中的字符串navigationProperty)
query=query.Include(navigationProperty);
if(orderBy!=null)
{
return wait orderBy(query.ToListAsync();
}
其他的
{
return wait query.ToListAsync();
}
}
服务层中的这段代码正在按id获取订单,但出于任何原因都不会返回相关的客户、OrderItem和产品实体:

public async Task<IEnumerable<Order>> GetOrdersAsync()
{
    return await _repository.GetAsync(null, q => q.OrderByDescending(p => p.CreatedDate), "Customer", "OrderItems", "OrderItems.Product");
}
public async Task<Order> GetOrderByIdAsync(long id)
{
  return await _repository.GetByIdAsync(id, "Customer", "OrderItems", "OrderItems.Product");
}
公共异步任务GetOrderByIdAsync(长id)
{
return wait_repository.GetByIdAsync(id,“Customer”,“OrderItems”,“OrderItems.Product”);
}
在存储库中:

public async Task<IEnumerable<Order>> GetAsync(Expression<Func<Order, bool>> where = null, Func<IQueryable<Order>, IOrderedQueryable<Order>> orderBy = null, params string[] navigationProperties)
{
    IQueryable<Order> query = _context.Set<Order>();

    if (where != null)
    {
        query = query.Where(where);
    }

    //Apply eager loading
    foreach (string navigationProperty in navigationProperties)
            query = query.Include(navigationProperty);

    if (orderBy != null)
    {
        return await orderBy(query).ToListAsync();
    }
    else
    {
        return await query.ToListAsync();
    }
}
public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
    DbSet<Order> dbSet = _context.Set<Order>();

    foreach (string navigationProperty in navigationProperties)
        dbSet.Include(navigationProperty);

    return await dbSet.FindAsync(id);
}
公共异步任务GetByIdAsync(长id,参数字符串[]navigationProperties)
{
DbSet DbSet=_context.Set();
foreach(navigationProperties中的字符串navigationProperty)
数据库集包括(导航属性);
返回等待dbSet.FindAsync(id);
}

我看到这两个存储库方法之间的一个区别是,一个是在包含导航属性之前将_context.Set()强制转换为IQueryable,而另一个是直接在DbSet本身上调用Include。这有关系吗

因此,不起作用的存储库方法使用的是DBSet.Include。Include实际上是DBQuery类上的一个方法,DBSet从中继承。我需要使用DBQuery.Include为我包含的每个导航属性使用新的查询实例替换查询。因此,我将实施更改为:

public async Task<Order> GetByIdAsync(long id, params string[] navigationProperties)
{
    DbQuery<Order> dbQuery = _context.Set<Order>();

    foreach (string navigationProperty in navigationProperties)
        dbQuery = dbQuery.Include(navigationProperty);

    return await dbQuery.Where(o => o.Id == id).FirstOrDefaultAsync();
}
公共异步任务GetByIdAsync(长id,参数字符串[]navigationProperties)
{
DbQuery DbQuery=_context.Set();
foreach(navigationProperties中的字符串navigationProperty)
dbQuery=dbQuery.Include(navigationProperty);
返回wait-dbQuery.Where(o=>o.Id==Id.FirstOrDefaultAsync();
}

实际上。。。我不太确定IQueryable是否具有DbSet所具有的相同Include()功能。