tags:

views:

33

answers:

2

I working on a project that uses NHibernate. My problem is when I'm trying to add an object that contains a list of objects I receive the following exception:

Illegal attempt to associate a collection with two open sessions from Session.Save method

I don't know what is the problem or how to solve it, please if any one can help I will appreciated.

A: 

It seems that objects you try to add were loaded from different session that the destination object into which you are attaching them.

Petr Kozelek
+1  A: 

You are having session management problems. It seems that you are using an object from one session as collection item in a collection from some other session. Using multiple parallell sessions is usually not a good practice (at least not from the same thread).

Read up on Unit of Work here; http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/04/10/nhibernate-and-the-unit-of-work-pattern.aspx .

There is an excellent and free implementation of this in Rhino.Commons, you can read more about that here: http://ayende.com/blog/archive/2007/06/08/rhino-commons-repositorylttgt-and-unit-of-work.aspx.

Andreas Paulsson