Title: NHibernate.LazyInitializationException
Tags: c#;asp.net-mvc;nhibernate;fluent-nhibernate;lazy-loading
Question: We have been having this issue pop up sporadically, but now I can reproduce it every time.  I am incrementing a view counter on my custom built forums, which causes an error:
NHibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
This error occurs on another collection in the object.  If I add:
.Not.LazyLoad()
To my Fluent mapping, the error shifts around my project.  I kept disabling lazy loading on objects intil it going to a spot where there was no lazy loading, and then it threw this error:
NHibernate.LazyInitializationException: Could not initialize proxy - no Session.
So, then I took out the nots on my lazy loading and now I'm back to square one.  It only errors when I increment this view counter.  Here is a snippet of my base class save code:
using (ISession session = GetSession())
using (ITransaction tx = session.BeginTransaction())
session.SaveOrUpdate(entity);
tx.Commit();
Looking around, I read in another post that the transactions can cause an issue, but that was because of where they were placed.  This code is extended to classes that are separate from my domain objects (repository classes).  Here is the post:
hibernate: LazyInitializationException: could not initialize proxy
I don't believe that is my issue here.  Here is my fluent mapping for the first collection that is throwing the error.  There are several other similar collections.
HasManyToMany(x =&gt; x.Votes)
.WithTableName("PostVotes")
.WithParentKeyColumn("PostId")
.WithChildKeyColumn("VoteId");
Comment: http://stackoverflow.com/questions/1117633/do-i-have-to-close-sessions-with-castles-isessionmanager-for-nhibernate My question is similar.

Here is the accepted answer: A little further research into this issue as it has reproduced itself is that in a single call back to the server, if you do a save and get, you need to flush the session.  I do my flush after the save and that seems to have corrected the problem.

Here is another answer: Check to see that there aren't any other objects loaded in that session.  I've had a similar situation occur in which I would call Save on an object that didn't have any Lazy Loading and it really sent me for a loop. Why would I get this error on an object that isn't lazy loading?
In my situation I was loading several other objects into the page and these object weren't mapped properly.  When I called Save NHibernate would try to synchronize in the session and throw the error.
Try removing all other NHibernate, leaving just this counter update.  If the error doesn't occur with just the counter slowly add back in you other calls until the error returns. Then you can start addressing the real culprit.
Comment for this answer: I tried your suggestion and it is erroring in the page code when I try to iterate one of the collections.  There is no lazy loading, yet it's still erroring.  If I remove the counter increment, the page works fine.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 2.5', 'CreationDate': '2009:00:07/14/09', 'FavoriteCount': 4.0, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc><nhibernate><fluent-nhibernate><lazy-loading>', 'Title': 'NHibernate.LazyInitializationException', 'ViewCount': 17244, 'id': 1125988, 'user_AboutMe': '<p>A man, when confronted with a problem, said "I know!  I\'ll use regular expressions!"</p>\n\n<p>Now he has two problems.</p>\n', 'user_AccountId': 37578.0, 'user_DisplayName': 'Josh', 'user_Reputation': 16058.0, 'user_Views': 1011.0, 'user_id': 107455.0}}