How to cascade evict an object from a session through Fluent NHibernate
I've got a nested object (object with subobjects) that I'd like to retrieve from a repository associated with an active session from one database, evict it, and save it in another database (different session, different connection string, different repository). I have tried:
myISession.Evict(myObjectInstance);
My MappingConfiguration has
Conventions.Add(DefaultCascade.All())
for all types.
But I still get "NHibernate.HibernateException: Illegal attempt to associate a collection with two open sessions". How can 开发者_如何学CI remove the association with the original session?
OK, it turns out my object, in this case myObjectInstance was composed of objects from two different sessions, let's call them A and B. myObjectInstance had properties myProp1 from session A, and myProp2 from session B. I was then to store the whole myObjectInstance in session B.
Unfortunately I was trying to evict myObjectInstance from session A, rather than just myProp1.
The solution was to:
NHibernateUtil.Initialize(myObjectInstance.myProp1); //eager load object being evicted.
_dataSession.Evict(myObjectInstance.myProp1);
MyObjectRepository.Save(myObjectInstance);
精彩评论