多个相关实体的预加载
我需要添加一个扩展方法,可以处理多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的最简单的方法是使用参数数组:
The simplest way I can think of would be to use a param array: