This is part of a series of posts about NHibernate Pitfalls. See the entire collection here.
Beware when accessing previously un-accessed lazy properties (simple properties, many to one, one to one, many to many, one to many) outside the scope of its originating session: it will result in an exception, because the session is no longer accessible.
One place where this commonly occurs is when returning entities from WCF web services and there are two ways to get over this:
- Force loading of all associations beforehand;
- Instantiating a custom Data Transfer Object with all required properties loaded.