I am using Entity Framework and a Repository Pattern for all my data access, when using table navigation I have noticed that 2 queries are being run when I got the first object and reference a field in a navigation object. As I have lots of relationships in the database using this technique for my navigation properties may cause performance overheads.
I have looked into the Include(string tableName)
method and this will work really well (if I was not using a generic RP) but this only takes one table name. I have managed to replicate this in my repository pattern for one include by changing my where from classs
to EntityObject
but how can I have multiple includes in one query using a repository pattern??
here is my code:
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);
}
}
You can chain your includes:
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);
}