問題は、すべての子供の年齢が特定の年齢を超えているすべての親を取得することです。予想される出力には、子供のリストとその親も含まれている必要があります。
私は次のものを持っています...
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 を使用して、オブジェクトを別の同様のオブジェクト (親と子) にマップします。コントラクト名前空間の親と子は、まったく同じタイプのプロパティを持っています