关于 c#:Multiple 包括使用实体框架和存储库模式
Multiple includes using Entity Framework and Repository Pattern
我正在使用实体框架和存储库模式进行所有数据访问,当使用表格导航时,我注意到当我获得第一个对象并引用导航对象中的字段时正在运行 2 个查询。由于我在数据库中有很多关系,将这种技术用于我的导航属性可能会导致性能开销。
我已经研究了
这是我的代码:
|
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) public IQueryable< T > FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate, param string[] include) |
您可以链接您的包含:
|
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); |
相关讨论
- 这不是编译,
query = query.Include(inc) IQueryable< T > 不包含名为Include 的方法。 -
您可能需要添加一个
using System.Data.Entity -
ObjectQuery<T> 也定义了方法,因此您可以用它替换IQueryable<T> 。你用的是什么版本的EF? - 我刚刚意识到它不存在,因为我使用的是旧版本的实体框架和 System.Data.Entity。我更新了版本,它现在可以工作了。
- 旧版本是什么版本,你升级到什么版本?