开发者

No current session context set error in my Nhibernate Test project

开发者 https://www.devze.com 2023-04-07 12:34 出处:网络
I\'m getting the error: No CurrentSessionContext configured (set the property current_session_context_class).

I'm getting the error:

No CurrentSessionContext configured (set the property current_session_context_class).

I'm not sure wha开发者_JAVA百科t to put there, I have this:

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

Then in my repository I just use the SessionFactory property in the helper.


in your "Fluently", before the ".Mappings(----) statement, you need to specify CurrentSessionContext. To do so, assuming you're using it in a Web Context,you would insert above the ".Mappings" line as shown below. (I've also modified retrieving connection strings' value, thanks to 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();
    }


I am guessing you are getting this property when you are trying to use sessionFactory.GetCurrentSesssion()

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

Also I would suggest you use sessionFactory.OpenSession()


For people using web session context: .CurrentSessionContext("web"), the session is stored in HttpContext.Items which will not exist for your unit tests.

.CurrentSessionContext("thread_static") can be used instead in unit tests.

0

精彩评论

暂无评论...
验证码 换一张
取 消