iam trying a new query with nhibernate and find a new problem :(
take this as model:
public class D { int id; }
public class C { int id; }
public class B {
int id;
ICollection<C> Cs;
ICollection<D> Ds;
}
public class A {
int id;
ICollection<B> Bs;
}
i want A object that have a particular B object and dinamically eager fetch Cs or Ds collection of selected B:
public virtual A Read(int idB, params Expression<Func<Attivita, object>>[] eagerFields)
i start with
IEnumerable<A> query = _session.QueryOver<A>()
.JoinQueryOver(a => a.Bs)
.Where(b => b.Id == idB)
.Future<A>();
foreach (Expression<Func<A>, object>> field in eagerFields)
_session.QueryOver<A>()
.Fetch(field).Eager
.Future<A>();
return query.First();
but eager load is not applyed: if i test this:
Read(12, a => a.Bs, a.Bs.First().Cs, a.Bs.First().Ds)
i see many query executed and Cs
and Ds
throw lazy inizializazion error
i found this and read that eager have problem without leftJoin so switch first part to this:
B BB= null;
IEnumerable<A> query =_session.QueryOver<A>()
.Fetch(a => a.Bs).Eager
.Left.JoinAlias(a => a.Bs, () => BB)
.Where(() => BB.Id == idB)
.Future<A>();
but have same problem
looking at similar fetch done in other case seem that possible cause can be a.Bs.First().Ds
as parameter selection for fetch
EDIT: just to clarify:
this works:
IEnumerable<A> query = _session.QueryOver<A>()
.Left.JoinAlias(a => a.Bs, () => BB)
.Where(() => BB.Id == IdB)
.Fetch(a => a.Bs).Eager
.Fetch(a => a.Bs.First().Cs).Eager
.Future<A>();
return query.First();
while this no:
IEnumerable<A> query = _session.QueryOver<A>()
.JoinQueryOver(a => a.Bs)
.Where(b => b.Id == idB)
.Future<A>();
foreach (Expression<Func<A>, object>> field in eagerFields)
_session.QueryOver<A>()
.Fetch(field).Eager
.Future<A>();
return query.First();
called in this way: Read(12, a => a.Bs, a.Bs.First().Cs, a.Bs.First().Ds)