0

我正在使用 NHibernate 和 FluentNH。

以下是四门课。

表单类型

  public class FormType
  {
    public virtual int Id
    { get; set; }

    public virtual string Name
    { get; set; }

    public virtual IList<KeyCompetency> KeyCompetencies 
    { get; set; }
  }

关键能力

  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; }
  }

子能力

  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();
}

我收到重复的关键能力、子能力记录。

4

1 回答 1

2

映射集合Set以避免此问题。请参阅此问题的答案。此外,由于您是按身份选择,因此最好使用.SingleOrDefault而不是FirstOrDefault.

于 2013-01-01T18:52:17.587 回答