添加包含到存储库

时间:2016-06-02 00:26:46

标签: c# entity-framework repository unit-of-work

我有一个工作存储库。

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;

    public Repository(DbContext context)
    {
        Context = context;
    }

    public TEntity Get(int id)
    {
        return Context.Set<TEntity>().Find(id);
    }

    public IEnumerable<TEntity> GetAll()
    {
        return Context.Set<TEntity>().ToList();
    }

    public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
    {
        return Context.Set<TEntity>().Where(predicate);
    }

    public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
    {
        return Context.Set<TEntity>().SingleOrDefault(predicate);
    }

    public void Add(TEntity entity)
    {
        Context.Set<TEntity>().Add(entity);
    }

    public void Remove(TEntity entity)
    {
        Context.Set<TEntity>().Remove(entity);
    }
}

正如我在编码存储库中读到的那样,在您真正需要它之前,您不会添加任何类。现在,我需要添加包含。我在这个社区Use Include() method in repository找到了这个:

public static class IncludeExtension
{
    public static IQueryable<TEntity> Include<TEntity>(this IDbSet<TEntity> dbSet,
                                                   params Expression<Func<TEntity, object>>[] includes)
                                                   where TEntity : class
    {
        IQueryable<TEntity> query = null;
        foreach (var include in includes)
        {
            query = dbSet.Include(include);
        }

        return query ?? dbSet;
    }
}

然后,我改变它以适应我的代码(我认为):

public IEnumerable<TEntity> Include(IDbSet<TEntity> dbSet,
                                    params Expression<Func<TEntity, object>>[] includes)
{
    IEnumerable<TEntity> query = null;
    foreach (var include in includes)
    {
        query = dbSet.Include(include);
    }

    return query ?? dbSet;
}

通过直接访问上下文,我可以写:

Provinces = _cmsDbContext.Provinces.Include(c => c.District).Include(c => c.District.Country).ToList();

但是,有了存储库,我无法写:

Provinces = Currentunitofwork.ProvinceRepository.Include(c => c.District).Include(c => c.District.Country).ToList();

我收到了错误:

cannot convert lambda expression to type IDbSet<Province> because it is not a delegate type

请问这里有什么问题。

2 个答案:

答案 0 :(得分:2)

我怀疑您的代码将lambda表达式传递给IDbSet参数,并且无法将其转换为该类型。

我无法测试但是编译,如果该方法是Repository类的成员,那么试试这个。

public IEnumerable<TEntity> Include(params Expression<Func<TEntity, object>>[] includes)
{
    IDbSet<TEntity> dbSet = Context.Set<TEntity>();

    IEnumerable<TEntity> query = null;
    foreach (var include in includes)
    {
        query = dbSet.Include(include);
    }

    return query ?? dbSet;
}

答案 1 :(得分:0)

再次感谢@Adam Carr。

这是方法代码:

public IQueryable<TEntity> Include(params Expression<Func<TEntity, object>>[] includeExpressions)
{
    IDbSet<TEntity> dbSet = Context.Set<TEntity>();

    IQueryable<TEntity> query = null;
    foreach (var includeExpression in includeExpressions)
    {
        query = dbSet.Include(includeExpression);
    }

    return query ?? dbSet;
}

我更改的是使用设置作为方法而不是属性。所以,而不是:

IDbSet<TEntity> dbSet = Context.Set<TEntity>;

我用过:

IDbSet<TEntity> dbSet = Context.Set<TEntity>();

另外,我使用 IQueryable 而不是 IEnumerable