C# 我的Nhibernate测试项目中没有当前会话上下文集错误

C# 我的Nhibernate测试项目中没有当前会话上下文集错误,c#,nhibernate,console-application,C#,Nhibernate,Console Application,我得到了一个错误: No CurrentSessionContext configured (set the property current_session_context_class). 我不知道该放什么,我有这个: public class NhDbHelper { public NhDbHelper() { CreateSessionFactory(); } private ISessi

我得到了一个错误:

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();
        }
    }
公共类NhDbHelper
{
公共NhDbHelper()
{
CreateSessionFactory();
}
私人ISessionFactory(sessionFactory);;
公共会话工厂会话工厂
{
获取{return\u sessionFactory;}
}
私有void CreateSessionFactory()
{
_sessionFactory=流利
.Configure()
.Database((MsSqlConfiguration.MsSql2008/)
.ConnectionString(@“服务器=。\SQLExpress;数据库=abc;Uid=sa;Pwd=123;”)
.ShowSql())
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf())
.ExposeConfiguration(cfg=>newschemaexport(cfg).Create(true,true))
.BuildSessionFactory();
}
}

然后,在我的存储库中,我只使用帮助程序中的SessionFactory属性。

我猜您在尝试使用SessionFactory.GetCurrentSession()时得到了这个属性。

另外,我建议您在“.Mappings”(--)语句之前的“fluntly”中使用
sessionFactory.OpenSession()

,您需要指定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();
    }
private void CreateSessionFactory()
{
_sessionFactory=流利
.Configure()
.Database((MsSqlConfiguration.MsSql2008/)
.ConnectionString(c=>c.FromConnectionString WithKey(“abc”))
.ShowSql())
.CurrentSessionContext(“web”)
.Mappings(m=>m.FluentMappings
.AddFromAssemblyOf())
.ExposeConfiguration(cfg=>newschemaexport(cfg).Create(true,true))
.BuildSessionFactory();
}

对于使用web会话上下文的用户:
.CurrentSessionContext(“web”)
,会话存储在HttpContext中。单元测试中不存在的项


.CurrentSessionContext(“thread\u static”)
可以在单元测试中使用。

我这样做了,但仍然会收到错误。我是否必须将它绑定到线程以便getcurrentsession工作?
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();
    }