C# 多种方法包括使用实体框架和存储库模式

C# 多种方法包括使用实体框架和存储库模式,c#,entity-framework,generics,repository-pattern,C#,Entity Framework,Generics,Repository Pattern,我使用实体框架和存储库模式进行所有数据访问,当使用表导航时,我注意到当我获得第一个对象并引用导航对象中的字段时,正在运行两个查询。因为我在数据库中有很多关系,所以将此技术用于导航属性可能会导致性能开销 我已经研究了Include(stringtablename)方法,这将非常有效(如果我没有使用通用RP),但这只需要一个表名。通过将where从classs更改为EntityObject,我成功地在我的存储库模式中为一个include复制了这一点,但如何使用存储库模式在一个查询中包含多个inclu

我使用实体框架和存储库模式进行所有数据访问,当使用表导航时,我注意到当我获得第一个对象并引用导航对象中的字段时,正在运行两个查询。因为我在数据库中有很多关系,所以将此技术用于导航属性可能会导致性能开销

我已经研究了
Include(stringtablename)
方法,这将非常有效(如果我没有使用通用RP),但这只需要一个表名。通过将where从
classs
更改为
EntityObject
,我成功地在我的存储库模式中为一个include复制了这一点,但如何使用存储库模式在一个查询中包含多个include

这是我的密码:

public class GenericRepository<T> : IRepository<T> where T : EntityObject, new()
{
    private Entities _Context;
    private ObjectSet<T> _ObjectSet;

    public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, string include)
    {
        // This works OK
        return this._ObjectSet.Include(include).Where(predicate);
    }

    public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include)
    {
        // This will not work but is what I am trying to do
        return this._ObjectSet.Include(include).Where(predicate);
    }
}
公共类GenericRepository:IRepository,其中T:EntityObject,new() { 私人实体(背景),; 私有对象集\u对象集; 公共IQueryable FindBy(System.Linq.Expressions.Expression谓词,字符串包含) { //这样行吗 返回此._ObjectSet.Include(Include).Where(谓词); } 公共IQueryable FindBy(System.Linq.Expressions.Expression谓词,参数字符串[]include) { //这是行不通的,但这正是我努力要做的 返回此._ObjectSet.Include(Include).Where(谓词); } }
您可以链接您的包括:

public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include)
{
    IQueryable<T> query = this._ObjectSet;
    foreach(string inc in include)
    {
       query = query.Include(inc);
    }

    return query.Where(predicate);
}
public IQueryable FindBy(System.Linq.Expressions.Expression谓词,参数字符串[]include)
{
IQueryable query=this.\u ObjectSet;
foreach(包括字符串公司)
{
query=query.Include(inc);
}
返回query.Where(谓词);
}

您可以链接您的包括:

public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include)
{
    IQueryable<T> query = this._ObjectSet;
    foreach(string inc in include)
    {
       query = query.Include(inc);
    }

    return query.Where(predicate);
}
public IQueryable FindBy(System.Linq.Expressions.Expression谓词,参数字符串[]include)
{
IQueryable query=this.\u ObjectSet;
foreach(包括字符串公司)
{
query=query.Include(inc);
}
返回query.Where(谓词);
}

这不是编译,
query=query.Include(inc)
IQueryable不包含名为
Include
的方法。您可能需要使用System.Data添加
。Entity
ObjectQuery
也定义了该方法,因此可以用该方法替换
IQueryable
。您使用的是什么版本的EF?我刚刚意识到它不存在,因为我使用的是旧版本的Entity Framework和System.Data.Entity。我更新了版本,现在可以使用了。旧版本是什么版本?您升级到了什么版本?这不是编译,
query=query.Include(inc)
IQueryable不包含名为
Include
的方法。您可能需要使用System.Data.Entity添加
ObjectQuery
也定义了方法,因此,您可以用它替换
IQueryable
。您使用的是什么版本的EF?我刚刚意识到它不存在,因为我使用的是旧版本的Entity Framework和System.Data.Entity。我更新了版本,现在可以使用了。旧版本是什么版本?您升级到了什么版本?