Im dealing with N+1 problem, and I want to eager load a collection in an especific query, but I am getting the "failed to lazily initialize a collection" error, or the query keep lazy loading.
I have tried many suggestions given by stackmembers but none worked.
My entity example:
public class Bar
{
private _IList<Foo> _fooList;
public IEnumerable<Foo> FooList {get{return _fooList;} }
}
The map example:
HasMany(d => d.FooList)
.Access.CamelCaseField(Prefix.Underscore)
.KeyColumn("IdBar")
.Cascade.AllDeleteOrphan()
.Inverse();
Queries that throws error (failed to lazily initialize a collection):
var test = GetSession().Query<Bar>()
.Fetch(b => b.FooList);
IList<Bar> bars= GetSession().QueryOver<Bar>()
.Fetch(d => d.FooList).Eager
.List();
Query that still with N+1 problem:
IList<Bar> bars= GetSession().QueryOver<Bar>()
.Fetch(d => d.FooList).Eager
.JoinQueryOver<Foo>(d => d.FooList)
.Where(f => f.Active).List<Bar>();
Foo fooAlias = null;
IList<Bar> bars= GetSession().QueryOver<Bar>()
.Fetch(d => d.FooList).Eager
.JoinAlias(d => d.FooList, () => fooAlias)
.List();
I need help to solve this N+1 problem, eager loading the FooList collection.