问题是获取所有孩子年龄都在一定年龄以上的所有父母。预期的输出还应该包括他们父母的孩子列表。
我有以下...
session.CreateQuery("select p, c from Parent as p left outer join p.Children as c where c.Age!= :age")
.SetParameter("age", somevalue);
但我收到以下错误:
Initializing[ns.Parent #18]-failed to lazily initialize a collection
of role: ns.Children, no session or session was closed
这是我的代码:
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());
我使用 AutoMapper 将对象映射到另一个类似的对象(父母和孩子)。Contract命名空间中的Parent和Child具有完全相同类型的属性