8

I am trying to use fluent with session per request. I am following a "recipe" from nhibernate cookbook however it uses the nhibernate config file.

I am not sure what is better but right now I am sticking with fluent config just because I would not know how to set the nhibernate config file to use fluent mapping and vanilla nhibernate mapping(hbm files).

namespace Demo.WebUI
{
    public class MvcApplication : NinjectHttpApplication
    {
        public static ISessionFactory SessionFactory { get; private set; }

        protected override void OnApplicationStarted()
        {
            SessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                    c => c.FromConnectionStringWithKey("test")))
                .Mappings(m => m.FluentMappings
                    .AddFromAssemblyOf
                     <Demo.Framework.Data.NhibernateMapping.UserMap>())
                .ExposeConfiguration(BuidSchema)
                .BuildSessionFactory();
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var session = SessionFactory.OpenSession();
            //CurrentSessionContext.Bind(session);
        }

        protected void Application_EndRequest(object sender, EventArgs e)
        {
            //var session = CurrentSessionContext.Unbind(SessionFactory);
            SessionFactory.Dispose();
        }
    }
}

As you can see in the Begin_Request the books tutorial had

CurrentSessionContext.Bind(session);

However if I use this it throws a error since I don't have the nhibernate config file in use.

So how do I change it to use fluent configuration? Or do I not even need to do this step?(ie is it done internally?)

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

13

You need to tell NHibernate how to handle the session context. The following might work:

Fluently.Configure()
        ...
        .ExposeConfiguration(cfg => cfg.SetProperty(
                                        Environment.CurrentSessionContextClass,
                                        "web")

Also, unrelated to this: you are disposing the SessionFactory on EndRequest. That is an error.

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Can you please elaborate on the part of "EndRequest is an error" I am just following what is done in nhibernate cookbook 3.0. – chobo2 Jan 16 '11 at 03:53
  • Ah I see what you mean. Ya I meant to end the current session not the session factory(that would be bad lol). – chobo2 Jan 16 '11 at 04:10
  • This helped me, and i also got a email from the founder of Hibernate telling me the same, to use 'web'. I think you also need to be sure when using GetCurrentSession and binding, that you are using WebSessionContext.Bind(session) and WebSessionContext.Unbind(sessionFactory); – Jeffrey Holmes Jan 26 '23 at 12:34
0

If your project is Asp.Net web site (not web application ), in Global.asax you should use like that

NHibernate.Cfg.Environment.CurrentSessionContextClass

himyata
  • 338
  • 1
  • 9