Reputation: 1841
I'm experiencing something rather odd. I'm tinkering with NHibernate 3.2 mapping by code and have a very simple object model I'm using just to play.
None of my properties in the entire model are marked as virtual because I dont want lazy loading. I'm mapping by code and in each class mapping I'm setting Lazy(false);
However when it comes to mapping collections, if I try and access a collection after the session has ended I get an error "failed to lazily initialize a collection of role..."
.
I have to explicitly set collectionMapping.Lazy(CollectionLazy.NoLazy);
before it will eager load the collection. It was my understanding that lazy loading was not possible unless your properties in your model were defined at virtuals?
Have I fundamentally missed something?
Upvotes: 1
Views: 2085
Reputation: 6876
Virtual properties and methods are only needed for lazy associations (many-to-one or one-to-one) because NHibnerate needs to set a proxy entity on the association property.
Collections (one-to-many and many-to-many) don't need any virtual properties because only the collection is lazy, not the entities in the collection. NHibernate will always use its own collection classes, even if you disable lazy loading.
Upvotes: 2
Reputation: 64628
You still need to use IList<T>
instead of List<T>
, because NH needs its own collection implementation.
Consider:
Upvotes: 1
Reputation: 190943
virtual
is needed more than just for lazy loading. NHibernate requires them to be virtual
because it creates a run-time proxy of the class and injects behavior.
Upvotes: 7