lomaxx
lomaxx

Reputation: 115793

NHibernate Session.Evict()

I don't have access to code here in front of me so I was just wondering if someone could help me out with Session.Evict().

Say I have a Person object with a child collection of Addresses. I populate the Person object from a session and lazy load the Addresses collection. I then call Session.Evict(personObject) to detach the Person object from the session. My question is, if I attempt to access the Addresses collection will it just return null, or will I get an exception because the NHibernate proxy can't find the associated session?

Upvotes: 9

Views: 12734

Answers (3)

Luke
Luke

Reputation: 2101

You will receive a NHibernate.LazyInitializationException.

Upvotes: 6

Danielg
Danielg

Reputation: 2699

If you cause the lazy load to happen before you evict the entity, then the collection will be accessible even after the eviction. However if you evict the entity and then try to lazy load the child collection you will get an exception.

Upvotes: 20

Damovisa
Damovisa

Reputation: 19423

I also don't have code in front of me, but from memory, you'll probably get an exception.

If you have lazy loading on and working, NHibernate will try to load the Addresses collection. It should never return an incorrect value/collection, it'll only complain when it can't load what's been asked.

Upvotes: 0

Related Questions