Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No current session context set error in my Nhibernate Test project

I'm getting the error:

No CurrentSessionContext configured (set the property current_session_context_class).

I'm not sure what 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.

like image 767
Blankman Avatar asked Oct 17 '25 13:10

Blankman


1 Answers

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();
    }
like image 71
Seyed Ketabchi Avatar answered Oct 19 '25 05:10

Seyed Ketabchi