2011-03-22 71 views
1

我收到這個錯誤,想知道是否有人有任何想法如何調試。Nhibernate - 未能初始化一個集合...沒有會話或會話被關閉

由於

初始化[GL.Objects.Profile`1 [[GL.Objects.Education.Education, GL.Objects,版本= 0.1.1.0, 文化=中性 公鑰= ebf25c7be4be0c91 ]]#289289]

-failed懶洋洋地初始化角色的集合:

GL.Objects.Profile`1 [GL.Objects.Education.Education, GL.Objects,版本= 0.1.1.0 , 文化=中性, 公鑰= ebf25c7be4be0c91]。Profilables,

沒有會話或會話已關閉」

啓動會話

 var watsonService = new WatsonService(); 

這段代碼保存實例化的對象。

watsonService.SaveEducation(e); 
    epf1.Profilables.Add(e); 
    watsonService.SaveEducationProfile(epf1); 
    epf2 = watsonService.GetEducationProfile(epf1.ID.Value); 

第一斷言通過就好了。 epf2有一個id並被實例化。

 Assert.AreEqual(epf1.ID, epf2.ID); 

此斷言失敗。 Profileables匹配一個組合表,這個ID被插入成功。但是現在,當我嘗試訪問集合中的第一個元素時,它失敗了。

 Assert.AreEqual(epf1.Profilables[0].ID, epf2.Profilables[0].ID); 

這是得到年代由GetEducationProfile方法調用的方法實現。

public T Get<T>(int id) where T : IDataObject 
{ 
    return (T)_session.Load(typeof(T), id, LockMode.Read); 
} 

這是在流利的映射文件的構造函數的定義。

public EducationProfileMap() 
    { 
     Table("Profile"); 
     Id(x => x.ID) 
     .Column("ProfileID") 
     .GeneratedBy 
     .HiLo(FluentConst.HILO_TABLE, 
       FluentConst.NEXTHI_COLUMN, 
       FluentConst.MAXLO, 
       String.Format(FluentConst.WHERE_FMT_STR, "Profile")); 

     Map(x => x.Type).CustomType<int>().Column("ProfileType"); 

     HasManyToMany(x => x.Profilables) 
        .ParentKeyColumn("ProfileID") 
        .ChildKeyColumn("EducationID") 
        .Cascade.All() 
        .Table("EducationProfile"); 
    } 
+0

會話管理可能是NHibernate的一大痛苦。延遲加載使得它更加複雜。 – 2011-03-23 22:56:59

回答

1

我的道歉沒有給予更多的相關信息的問題。 問題來自於如何管理會話。

我的測試實例下面的類:

public WatsonAdaptor(string user) 
    { 
     if (DataSession == null) 
     DataSession = new HibernateSession(
         HibernateFactoryManager.HibernateFactory.Watson, user); 
    } 

以下方法由SaveEducation方法

public int? SaveEducation(Education e) { 
     try 
     { 
      var watson = new WatsonAdaptor("ealite"); 
      watson.Save(e); 
      return e.ID; 
     } 
     catch (Exception ex) 

打開一個會議的要求。

在返回時會話被拋棄,所以懶惰的加載不會發生。

我需要做的是確保在會話仍處於打開狀態時發生延遲加載。

相關問題