我正在使用NHibernate和FluentNH。
以下是四个班级。
FormType
public class FormType
{
public virtual int Id
{ get; set; }
public virtual string Name
{ get; set; }
public virtual IList<KeyCompetency> KeyCompetencies
{ get; set; }
}
KeyCompetency
public class KeyCompetency
{
public virtual int Id
{ get; set; }
public virtual string Name
{ get; set; }
public virtual FormType FormType
{ get; set; }
public virtual IList<SubCompetency> SubCompetencies
{ get; set; }
}
SubCompetency
public class SubCompetency
{
public virtual int Id
{ get; set; }
public virtual string Name
{ get; set; }
public virtual KeyCompetency KeyCompetency
{ get; set; }
public virtual IList<Ability> Abilities
{ get; set; }
}
能力
public class Ability
{
public virtual int Id
{ get; set; }
public virtual string Description
{ get; set; }
public virtual SubCompetency SubCompetency
{ get; set; }
}
我试图加载基于id的表单类型。
using (var session = DataContext.OpenSession())
{
return session.Query<FormType>()
.Where(x => x.Id == 1)
.FetchMany(x => x.KeyCompetencies)
.ThenFetchMany(x => x.SubCompetencies)
.ThenFetchMany(x => x.Abilities)
.ToList().FirstOrDefault();
}
我获得了关键能力,子能力的重复记录。
答案 0 :(得分:2)
将集合映射为Set
以避免此问题。请参阅this question的答案。此外,由于您是按身份进行选择,因此最好使用.SingleOrDefault
代替FirstOrDefault
。