2011-09-24 39 views
3

,我發現了錯誤:在我NHibernate的測試項目沒有當前會話的上下文設置錯誤

No CurrentSessionContext configured (set the property current_session_context_class). 

我不知道要放什麼東西,我有這樣的:

public class NhDbHelper 
    { 

     public NhDbHelper() 
     { 
      CreateSessionFactory(); 
     } 

     private ISessionFactory _sessionFactory; 

     public ISessionFactory SessionFactory 
     { 
      get { return _sessionFactory; } 
     } 


     private void CreateSessionFactory() 
     { 
      _sessionFactory = Fluently 
        .Configure() 
        .Database((MsSqlConfiguration.MsSql2008 // 
          .ConnectionString(@"Server=.\SQLExpress;Database=abc;Uid=sa;Pwd=123;") 
          .ShowSql())) 
        .Mappings(m => m.FluentMappings 
        .AddFromAssemblyOf<UserMap>()) 
        .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) 
        .BuildSessionFactory(); 
     } 
    } 

然後在我的倉庫中,我只是在助手中使用SessionFactory屬性。

+0

http://stackoverflow.com/questions/7454589/no-session-bound-to-the-current-context/7458905#7458905 –

回答

1

我猜你得到,當你試圖使用sessionFactory.GetCurrentSesssion()

_config.ExposeConfiguration(cfg => cfg.Properties.Add("current_session_context_class", "thread")); 

而且我會建議你使用你的「流利」 sessionFactory.OpenSession()

+0

我這樣做,仍然得到這個錯誤。我是否必須將它綁定到線程,以便getcurrentsession起作用? – Blankman

2

,在」 .Mappings之前(此屬性 - ---)語句,你需要指定CurrentSessionContext。爲此,假設你在Web上下文中使用它,你可以在上面插入「.Mappings」行,如下所示(我也修改了檢索連接字符串'價值,感謝Fluent:

private void CreateSessionFactory() 
    { 
     _sessionFactory = Fluently 
       .Configure() 
       .Database((MsSqlConfiguration.MsSql2008 // 
         .ConnectionString(c=>c.FromConnectionStringWithKey("abc")) 
         .ShowSql())) 
       .CurrentSessionContext("web") 
       .Mappings(m => m.FluentMappings 
       .AddFromAssemblyOf<UserMap>()) 
       .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) 
       .BuildSessionFactory(); 
    } 
0

對於使用Web會話上下文的人:.CurrentSessionContext("web"),會話存儲在HttpContext.Items中,這對於單元測試不存在。

.CurrentSessionContext("thread_static")可以在單元測試中使用。

相關問題