多个相关实体的预加载

发布于 2024-12-08 09:28:21 字数 1862 浏览 0 评论 0原文

我需要添加一个扩展方法,可以处理多个 Include 到已存在的 Repository 类:

public abstract class Repository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
    where TEntity : class
{
    private MyContext _dbContext;
    private readonly DbSet<TEntity> _dbSet;

    public IDatabaseFactory DatabaseFactory { get; private set; }

    protected Repository(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        _dbSet = Context.Set<TEntity>();         
    }

    protected MyContext Context
    {
        get { return _dbContext ?? (_dbContext = DatabaseFactory.Get()); }
    }

    public IQueryable<TEntity> Include(Expression<Func<TEntity, object>> path)
    {
        return _dbSet.Include(path);
    }
}

当前实现只能处理一个相关实体。我还需要能够立即加载两个或多个相关实体。

我的实体类:

public class Employee
{
    public int EmployeeId { get; set; }

    public virtual ICollection<Adjustment> Adjustments { get; set; }

    public int? TotalAdjustmentsPerEmployeeId { get; set; }
    public virtual TotalAdjustmentsPerEmployee TotalAdjustmentPerEmployee { get; set; }
}

我将在哪里使用它:

 public class EmployeeRepository : Repository<Employee, int>, IEmployeeRepository
 {
    public EmployeeRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
    { }

    public IEnumerable<Employee> GetAll(int companyID)
    {
       /** 
       *    I need to load employees based on their companyID and eager load 
       *    related TotalAdjustmentPerEmployee and Adjustments entities something like:



       *   return this.Include(a => a.TotalAdjustmentPerEmployee)
       *        .Include(a => a.Adjustments)
       *        .Where(e => e.CompanyId == companyID);
       */
    }
}

谢谢!

I need to add an extension method that can handle multiple Include to an already existing Repository class:

public abstract class Repository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey>
    where TEntity : class
{
    private MyContext _dbContext;
    private readonly DbSet<TEntity> _dbSet;

    public IDatabaseFactory DatabaseFactory { get; private set; }

    protected Repository(IDatabaseFactory databaseFactory)
    {
        DatabaseFactory = databaseFactory;
        _dbSet = Context.Set<TEntity>();         
    }

    protected MyContext Context
    {
        get { return _dbContext ?? (_dbContext = DatabaseFactory.Get()); }
    }

    public IQueryable<TEntity> Include(Expression<Func<TEntity, object>> path)
    {
        return _dbSet.Include(path);
    }
}

The current implementation can only handle one related entity. I need to be able to eager load two or more related entites as well.

my entity class:

public class Employee
{
    public int EmployeeId { get; set; }

    public virtual ICollection<Adjustment> Adjustments { get; set; }

    public int? TotalAdjustmentsPerEmployeeId { get; set; }
    public virtual TotalAdjustmentsPerEmployee TotalAdjustmentPerEmployee { get; set; }
}

Where I will use it:

 public class EmployeeRepository : Repository<Employee, int>, IEmployeeRepository
 {
    public EmployeeRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
    { }

    public IEnumerable<Employee> GetAll(int companyID)
    {
       /** 
       *    I need to load employees based on their companyID and eager load 
       *    related TotalAdjustmentPerEmployee and Adjustments entities something like:



       *   return this.Include(a => a.TotalAdjustmentPerEmployee)
       *        .Include(a => a.Adjustments)
       *        .Where(e => e.CompanyId == companyID);
       */
    }
}

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风不识月 2024-12-15 09:28:21

我能想到的最简单的方法是使用参数数组:

public IQueryable<TEntity> Include<TInclude>(params Expression<Func<TEntity, TInclude>>[] paths) 
{ 
    IQueryable<TEntity> results = _dbset;
    paths.ToList().ForEach(x => results = results.Include(x));
    return results
} 

The simplest way I can think of would be to use a param array:

public IQueryable<TEntity> Include<TInclude>(params Expression<Func<TEntity, TInclude>>[] paths) 
{ 
    IQueryable<TEntity> results = _dbset;
    paths.ToList().ForEach(x => results = results.Include(x));
    return results
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文