NHibernate - LAZY LOADING PROBLEM -Initializing[]-Could not initialize proxy - no Session."}
You are correct. Because the NHibernate Session is closed in your GetAccount method (it is only open in the scope of the using
statement), you cannot load additional objects outside of this method. There are 2 potential fixes:
- Create the session at the operation level (i.e. in the method containing the problem code), then use this session in the get & save methods. You can use the session by passing it in as a parameter to the methods.
- Change the object to not use lazy loading. You can do this by adding
.Not.LazyLoad()
to theStatus
object in your fluent mapping.
I find the easiest way to turn off lazy loading is to add a DefaultLazy convention, i.e.:
.Conventions.Add( DefaultCascade.All(), DefaultLazy.Never() )
Note that turning lazy loading on (DefaultLazy.Always()) can really increase performance, depending on your application.
The downside is, you always have to have a session open before you can lazy load the rest of the data in an entity. Session management to support lazy loading is one of the big pain points with NHibernate, in my experience.