Fluent NHibernate Configuration for Session Context

679 views
Skip to first unread message

Action Jackson

unread,
Mar 29, 2009, 11:56:23 PM3/29/09
to Fluent NHibernate

I apologize in advance if this question has been answered many times
over. How can I specify the session context using the Fluent
configuration? In other words, what is the Fluent translation of a
traditional xml mapping like "<property
name="current_session_context_class">thread_static</property>"?

Paul Batum

unread,
Mar 31, 2009, 7:17:10 AM3/31/09
to fluent-n...@googlegroups.com
You set this on your NHibernate.Cfg.Configuration instance right?

You should be able to use the ExposeConfiguration method to get access to the underlying Configuration instance and then set the session context appropriately. Or alternatively you can build your Configuration instance as you normally would including setting the session context, and then pass it in as an argument to Fluently.Configure().

Paul Batum

James Gregory

unread,
Mar 31, 2009, 7:21:20 AM3/31/09
to fluent-n...@googlegroups.com
If you're using Fluently.Configure there's also a Raw method, which'll allow you to set any property that isn't already exposed.

Action Jackson

unread,
Mar 31, 2009, 2:37:54 PM3/31/09
to Fluent NHibernate

Paul and James, thank you very much for your responses. I have
tried these approaches, but with no luck. I'm noticing an issue that
is very interesting. Hopefully, something jumps out at you right
away. Here's an example of the code which presides in a class I call
'NHibernateHelper' (borrowed from NH in Action):

_cfg = new Configuration()
.SetNamingStrategy( naming strategy class here)
.SetProperty("current_session_context_class",
"thread_static");

// NOTE: At this point, the _cfg configuration object has 8 properties
including the current session context setting. The interesting part
comes next:

_sessionFactory = Fluently
.Configure(_cfg)
.Database
(
MsSqlConfiguration.MsSql2005
.ConnectionString(c =>
c.FromConnectionStringWithKey("conn"))
)
.Mappings
(
x => x.FluentMappings
.AddFromAssembly(...blah blah blah...)
)
.BuildSessionFactory();

After executing this piece of logic, the number of properties of the
_cfg configuration object has been reduced from 8 to 5 and the session
context setting is gone. Consequently, a piece of logic like this
fails since there is no session context:

try
{
using (ISession session =
NHibernateHelper.GetCurrentSession())
{
using (session.BeginTransaction())
{
CurrentSessionContext.Bind(session);

session.Save(person);
}
}
}
finally
{
CurrentSessionContext.Unbind
(NHibernateHelper.SessionFactory);
}

Any ideas? Thanks again for your help thus far!

On Mar 31, 6:21 am, James Gregory <jagregory....@gmail.com> wrote:
> If you're using Fluently.Configure there's also a Raw method, which'll allow
> you to set any property that isn't already exposed.
>
>
>
> On Tue, Mar 31, 2009 at 12:17 PM, Paul Batum <paul.ba...@gmail.com> wrote:
> > You set this on your NHibernate.Cfg.Configuration instance right?
>
> > You should be able to use the ExposeConfiguration method to get access to
> > the underlying Configuration instance and then set the session context
> > appropriately. Or alternatively you can build your Configuration instance as
> > you normally would including setting the session context, and then pass it
> > in as an argument to Fluently.Configure().
>
> > Paul Batum
>
> > On Mon, Mar 30, 2009 at 2:56 PM, Action Jackson <crjac...@gmail.com>wrote:
>
> >>  I apologize in advance if this question has been answered many times
> >> over.  How can I specify the session context using the Fluent
> >> configuration?  In other words, what is the Fluent translation of a
> >> traditional xml mapping like "<property
> >> name="current_session_context_class">thread_static</property>"?- Hide quoted text -
>
> - Show quoted text -

epitka

unread,
Mar 31, 2009, 2:55:11 PM3/31/09
to Fluent NHibernate
Did you try ExposeConfiguration like this ?

_sessionFactory = Fluently
.Configure(_cfg)
.ExposeConfiguration(c=>c.SetNamingStrategy
( naming strategy class here)
.SetProperty("thread_static");
> .Database
> (
> MsSqlConfiguration.MsSql2005
> .ConnectionString(c =>
> c.FromConnectionStringWithKey("conn"))
> )
> .Mappings
> (
> x => x.FluentMappings
> .AddFromAssembly(...blah blah blah...)
> )
> .BuildSessionFactory();

Action Jackson

unread,
Mar 31, 2009, 7:54:40 PM3/31/09
to Fluent NHibernate
Hey, thank you very much for your response. However, your
suggestion to use ExposeConfiguration() doesn't seem to be working
either. I've tried variations of potential solutions that use
ExposeConfiguration(). Still no luck. Would you be kind enough to
provide the full block of code that works for you? I'm still noticing
the "property count reduction".
> > > - Show quoted text -- Hide quoted text -

Action Jackson

unread,
Mar 31, 2009, 11:50:47 PM3/31/09
to Fluent NHibernate
Actually, after several tweaks, I think I finally got it. Here's what
I came up with:

_sessionFactory = Fluently
.Configure()
.Database
(
MsSqlConfiguration
.MsSql2005
.ConnectionString(c => c.FromConnectionStringWithKey
("conn"))
)
.Mappings( x =>
{
x.FluentMappings
.AddFromAssembly(Assembly.LoadFrom(blah blah...))
.ConventionDiscovery.AddFromAssemblyOf<blah blah...>();
})
.ExposeConfiguration( x =>
{
x.SetProperty("current_session_context_class",
"thread_static");
})
.BuildConfiguration()
.BuildSessionFactory();

It's all clear to me now...thank you to all that replied. Hopefully,
this post helps others out there.
Reply all
Reply to author
Forward
0 new messages