When exactly does NHibernate convert a collection that, while the Session is open is represented as a PersistentGenericBag, but later on is converted back to the IList that I defined in my source code?
I thought it was when either the transaction was committed, or the session was disconnected. But I’m writing a unit test and no matter how many things I close, flush, commit and disconnect, my object always has a PersistentGenericBag, not a List.
Thanks
Do not get me wrong. But do you really have mapping like this?
Because my experience says, that this will end up in exception like this:
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches);1[System.String]`setter of MyLib.MyObject ---> System.InvalidCastException: Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag'1[System.String]' to type 'System.Collections.Generic.List
What I would expect inside your mapping is IList<>. Because exactly this will allow the NHibernate to inject its own implementation of it. Namely:
PersistentGenericBagAnd in that case, the answer is: never. The proxy of MyObject provides all functionality published by IList<> while still working with property of type
PersistentGenericBagAnd when is it really populated? when firstly accessed (via getter). Inner implementation of
PersistentGenericBagwill execute sql query and load data into internal IList<>. Of course session have to be still opened and connected to MyObject