我收到了错误:
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 :(得分:3)
在“Fluently”中,在“.Mappings(----)语句之前,您需要指定CurrentSessionContext。为此,假设您在Web上下文中使用它,您将在”。映射“如下所示。(我还修改了检索连接字符串的值,感谢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();
}
答案 1 :(得分:1)
当您尝试使用sessionFactory.GetCurrentSesssion()
_config.ExposeConfiguration(cfg => cfg.Properties.Add("current_session_context_class", "thread"));
我还建议您使用sessionFactory.OpenSession()
答案 2 :(得分:0)
对于使用网络会话上下文的人:.CurrentSessionContext("web")
,会话存储在HttpContext.Items中,单元测试不存在。
.CurrentSessionContext("thread_static")
可以在单元测试中使用。