The question is to get all parents whose all children age is above a certain age.The expected output should also include the children list with their parent.
I have the following...
session.CreateQuery("select p, c from Parent as p left outer join p.Children as c where c.Age!= :age")
.SetParameter("age", somevalue);
But I am getting the following error:
Initializing[ns.Parent #18]-failed to lazily initialize a collection
of role: ns.Children, no session or session was closed
This is my code:
class Parent {
public virtual int Id { get; set; }
public virtual IList<Child> Children { get; set; }
}
class Child {
public virtual int Id { get; set; }
public virtual int Age{ get; set; }
public virtual int ParentId { get; set; }
}
//the mapping
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
this.Id(t => t.Id);
this.HasMany(t => t.Child).Not.LazyLoad();
}
}
class ParentRepository : IParentRepository {
public IEnumerable<Parent> GetAll()
{
using (var session = _factory.OpenSession())
{
session.CreateQuery("select p, c from Parent as p left outer join p.Children as c where c.Age!= :age")
.SetParameter("age", somevalue);
return result.Distinct().ToArray();
}
}
}
//In a different class I call GetAll.
var data = parentRepository.GetAll();
//at the following line that i get the error.
IEnumerable<Contracts.Parent> parents = Mapper.Map<IEnumerable<ns.Parent>, IEnumerable<Contracts.Parent>>(data.ToArray());
I use AutoMapper to map the object to another similar objects(Parent and Child). The Parent and the Child in the Contract namespace has exactly the same type of properties