私は、このリポジトリを持ってEF eager loadingが期待どおりに機能しないのはなぜですか?
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のかのようですは無視されています。
それ以上の方法では正常に動作します:私は変更する場合D:
含まないない結果を引き戻すが、var a = this.Repository.GetAll(new string[] { "ForbiddenUsers", "ForbiddenGroups" }).ToList();
が含まれています:
public IEnumerable<TEntity> GetAll(string include)
{
return this.dbEntitySet.Include(include);
}
はそれを呼び出すために、私は基本的にこれを行います
var a = this.Repository.GetAll("ForbiddenUsers").ToList();
これは問題なく動作します。
誰かに解決策を教えてもらえますか?
+1のため:私はこのようなラムダ式を使用します、あなたの代わりに
:
正しいコードは以下の通りですGetAllメソッドのアイデア!有用! – statue