In my app, I set up a ternary dictionary mapping so that for a given user, I can retrieve "settings" for each instance of an object that belongs to the user. That is, I have something like:
public class User
{
public virtual IDictionary<Baz, BazSettings> BazSettings { get; set; }
//...
So whenever I have a Baz
object, I can lookup the current user's baz settings via currentUser.BazSettings[baz]
.
I would like to be able to use a stateless session to do this, but I get a LazyInitializationException
with this code:
//int bazId;
using (IStatelessSession session = Global.SessionFactory.OpenStatelessSession())
{
var currentUser = session.Get<User>(Membership.GetUser().ProviderUserKey);
var baz = session.Get<Baz>(bazId);
var bazSettings = currentUser.BazSettings[baz]; // raises `LazyInitializationException`
When I use instead an ISession
, then the problem goes away.
The full NHibernate error message includes the text "no session or session was closed". This makes sense because when using a stateless session, entities are not connected to the session. However, I would think that there is a way to use the stateless session to perform this lookup.
How do I use a stateless session to perform the lookup currentUser.BazSettings[baz]
?