3

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.

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • http://stackoverflow.com/questions/7454589/no-session-bound-to-the-current-context/7458905#7458905 – Cole W Sep 25 '11 at 20:40

3 Answers3

3

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

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

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • I did that, still getting that error. Do I have to bind it to the thread so getcurrentsession works? – Blankman Sep 24 '11 at 20:42
0

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.

kravits88
  • 12,431
  • 1
  • 51
  • 53