I'm using Entity Framework 5 and have a generic repository, within which are several methods like the two Get() methods below:
public TEntity GetById(int id)
{
return DbSet.Find(id);
}
public TEntity Get(
Expression<Func<TEntity, bool>> filter = null,
IEnumerable<string> includePaths = null)
{
IQueryable<TEntity> query = DbSet;
if (filter != null)
{
query = query.Where(filter);
}
if (includePaths != null)
{
query = includePaths.Aggregate(query, (current, includePath) => current.Include(includePath));
}
return query.SingleOrDefault();
}
These are both very helpful, however when I want to make a slightly more complex GetById() call and retrieve some entity references at the same time, like so:
var user = _userRepository.GetById(
id,
new List<string> { "Roles", "Invoices" });
I end up having to roll out entity-specific (so non-generic) GetById(id, includes) calls for each entity so that I can access their specific Id fields in the lambda, i.e. UserId, or InvoiceId etc.
public User GetById(
int id,
IEnumerable<string> includes)
{
return Get(
(u => u.UserId == id),
includes);
}
It seems that I can't, with my average EF skills, work out how to combine the goodness of DbSet.Find(id) with the .Include() call in a generic fashion.
So the question is - is there a way to write a generic EF method that I can use to get an entity by it's id and include some references, and in turn remove the need to write entity specific GetById(id, includes) calls like I've done above.
Thanks in advance.
Find
andInclude
. – Mcintyre