2013-01-01 174 views
0

我使用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的formtype。

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

我得到關鍵能力,子能力的重複記錄。

回答

2

將收藏映射爲Set以避免此問題。請參閱this question的答案。另外,由於您是通過身份進行選擇,最好使用.SingleOrDefault而不是FirstOrDefault