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

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

问题描述

我使用实体框架code首先顶部的通用Repository模式。直到我需要在查询中包括多个实体一切工作的罚款。我得到了包括成功的一个实体,但现在我无法弄清楚如何将多个实体。查看一下我到目前为止有:

 公开的IQueryable< TEntity> GetQuery< TEntity>()其中TEntity:类
{
    VAR的entityName = GetEntityName< TEntity>();
    返回_objectContext.CreateQuery< TEntity>(的entityName);
}公众的IList< TEntity> GetQueryWithInclude< TEntity>(串toInclude)其中TEntity:类
{
    VAR的entityName = GetEntityName< TEntity>();
    返回_objectContext.CreateQuery< TEntity>(的entityName).INCLUDE(toInclude).ToList();
}私人字符串GetEntityName< TEntity>()其中TEntity:类
{
    返回的String.Format({0} {1},_objectContext.DefaultContainerName,_pluralizer.Pluralize(typeof运算(TEntity)已.Name点));
}

我试图这样做,但没有工作是传递一个字符串数组到函数,然后尝试追加的包括查询的顶部。我在想,如果我叫GetQueryWithInclude并在同一时间聚集查询的结果通过了一项实体名称(实际上是一个导航属性​​),但我担心这可能会重复在每次调用查询的结果...你认为将得到这个工作的最佳方式?

在此先感谢!

更新:

这里是我想要达到一个例子:

 公开IQueryable的GetQueryWithIncludes(字符串[] otherEntities)
{
    VAR的entityName = GetEntityName< TEntity>();
    //现在环比otherEntities阵列
    //并追加包括扩展查询
    //所以在循环中,是这样的:
    _objectContext.GetQuery< TEntity>(的entityName).INCLUDE(otherEntities [指数]);
}


解决方案

使用只是包含在IQueryable的扩展。这是EF 4.1装配使用。如果你不想引用装配在上层的数据访问组件创建包装扩展方法。

在这里,您有例如:

 公共静态的IQueryable< T> IncludeMultiple< T>(这IQueryable的< T>的查询,则params防爆pression<&Func键LT; T,对象>> []包括)
    其中T:类
{
    如果(包括!= NULL)
    {
        查询= includes.Aggregate(查询,
                  (目前包括)=> current.Include(含));
    }    返回查询;
}

您将使用它,例如像:

  VAR的查询= context.Customers
                   .IncludeMultiple(
                       C => c.Address,
                       C => c.Orders.Select(O => o.OrderItems));

与他们的地址和订单,每个订单将包含其订单项目此查询时,所有的客户。

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));
}

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?

Thanks in advance!

UPDATE:

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]);
}

解决方案

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.

Here you have example:

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;
}

You will use it for example like:

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

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

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

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