EF 包括其他实体(通用存储库模式) [英] EF Including Other Entities (Generic Repository pattern)

查看:20
本文介绍了EF 包括其他实体(通用存储库模式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Entity Framework Code First 之上使用通用存储库模式.一切正常,直到我需要在查询中包含更多实体.我必须成功包含一个实体,但现在我不知道如何包含多个实体.看看我到目前为止有什么:

I am using the Generic Repository pattern on top of Entity Framework Code First. Everything was working fine until I needed to include more entities in a query. I got to include one entity successfully, but now I can't figure out how to include multiple entities. Check out what I've got so far:

public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
{
    var entityName = GetEntityName<TEntity>();
    return _objectContext.CreateQuery<TEntity>(entityName);
}

public IList<TEntity> GetQueryWithInclude<TEntity>(string toInclude) where TEntity : class
{
    var entityName = GetEntityName<TEntity>();
    return _objectContext.CreateQuery<TEntity>(entityName).Include(toInclude).ToList();
}

private string GetEntityName<TEntity>() where TEntity : class
{
    return string.Format("{0}.{1}", _objectContext.DefaultContainerName, _pluralizer.Pluralize(typeof(TEntity).Name));
}

我尝试做但没有奏效的是将字符串数组传递到函数中,然后尝试将包含的内容附加"到查询的顶部.我想知道如果我一次调用 GetQueryWithInclude 并传递一个实体名称(实际上是一个导航属性)来聚合查询的结果会怎样,但我担心这可能会在每次调用时重复查询的结果......您认为让它发挥作用的最佳方式是什么?

What I tried to do but didn't work was pass in an array of strings into a function, then try to "append" the includes on top of the query. I was wondering what if I called the GetQueryWithInclude and passed an entity name (actually a navigation property) at a time to aggregate the results of the query, but I'm worried this might duplicate the results of the query on each call... What do you think would be the best way to get this to work?

提前致谢!

更新:

这是我要实现的目标的示例:

Here's an example of what I'm trying to achieve:

public IQueryable GetQueryWithIncludes(string[] otherEntities)
{
    var entityName = GetEntityName<TEntity>();
    //now loop over the otherEntities array 
    //and append Include extensions to the query
    //so inside the loop, something like: 
    _objectContext.GetQuery<TEntity>(entityName).Include(otherEntities[index]);
}

推荐答案

在 IQueryable 上仅使用 Include 扩展.它在 EF 4.1 程序集中可用.如果您不想在上层引用该程序集,请在数据访问程序集中创建包装器扩展方法.

Use just the Include extension on IQueryable. It is available in EF 4.1 assembly. If you don't want to reference that assembly in your upper layers create wrapper extension method in your data access assembly.

这里有示例:

public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params Expression<Func<T, object>>[] includes)
    where T : class
{
    if (includes != null)
    {
        query = includes.Aggregate(query, 
                  (current, include) => current.Include(include));
    }

    return query;
}

您将使用它,例如:

var query = context.Customers
                   .IncludeMultiple(
                       c => c.Address,
                       c => c.Orders.Select(o => o.OrderItems));

此查询将加载所有客户的地址和订单,并且每个订单都将包含其订单项目.

This query will load all customers with their addresses and orders and every order will contain its order items.

这篇关于EF 包括其他实体(通用存储库模式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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