【发布时间】:2014-01-15 16:28:36
【问题描述】:
我有这个仓库,
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext context;
private readonly DbSet<TEntity> dbEntitySet;
public Repository(DbContext context)
{
if (context == null)
throw new ArgumentNullException("context");
this.context = context;
this.dbEntitySet = context.Set<TEntity>();
}
public IEnumerable<TEntity> GetAll()
{
return this.dbEntitySet;
}
public IEnumerable<TEntity> GetAll(string include)
{
return this.dbEntitySet.Include(include);
}
public IEnumerable<TEntity> GetAll(string[] includes)
{
foreach (var include in includes)
this.dbEntitySet.Include(include);
return this.dbEntitySet;
}
public void Create(TEntity model)
{
this.dbEntitySet.Add(model);
}
public void Update(TEntity model)
{
this.context.Entry<TEntity>(model).State = EntityState.Modified;
}
public void Remove(TEntity model)
{
this.context.Entry<TEntity>(model).State = EntityState.Deleted;
}
public void Dispose()
{
this.context.Dispose();
}
}
我遇到的问题是这种方法:
public IEnumerable<TEntity> GetAll(string[] includes)
{
foreach (var include in includes)
this.dbEntitySet.Include(include);
return this.dbEntitySet;
}
当我运行它并在返回之前放置一个断点时,就好像 foreach 被忽略了。
上面的方法效果很好:
public IEnumerable<TEntity> GetAll(string include)
{
return this.dbEntitySet.Include(include);
}
要调用它,我基本上是这样做的:
var a = this.Repository.GetAll(new string[] { "ForbiddenUsers", "ForbiddenGroups" }).ToList();
返回结果但不包括包含:D 如果我将调用修改为:
var a = this.Repository.GetAll("ForbiddenUsers").ToList();
它工作正常。
谁能给我一个解决方案?
【问题讨论】:
-
+1 表示 GetAll 方法的想法!有用!
标签: c# entity-framework eager-loading