title: NHibernate: Illegal attempt to associate a collection with two open sessions
body: <p>I'm having trouble with NHibernate, First let me say I'm not native English talker, OK, let get to point.</p>
  
  <p>As i face this error, i searched over the internet, and saw lot of people talking about it, but most of them had issue with cascade attribute, and totally if i wanna say, non matched my case, or even if it did i couldn't get through.</p>
  
  <p>My Database is so small, but a bit complex.
  My model files generated using Entity Developer.
  <img src="https://i.stack.imgur.com/ugnwQ.jpg" alt="enter image description here"></p>
  
  <p>As you can see there is inheritance, one-to-many and many-to-many relation ships,</p>
  
  <p>As I am new to NHibernate, i write a test code, my normal test worked, till i add this many-to-many relation ship.</p>
  
  <p>So i write this code:</p>
  
  <pre><code>//Owner o = new Owner
          //{
          //    //OwnerId = Guid.NewGuid(),
          //    CellPhone1 = null,
          //    CellPhone2 = "09132198895",
          //    Phone = "03114335502",
          //    Firstname = "Hassan",
          //    Lastname = "Faghihi",
          //    OwnerTitle = PersonTitle.Mr
          //};
  
          //OwnerFactory ownerFactory = new OwnerFactory();
          //ownerFactory.SaveOwner(o);
  
          //SellHouse sh = new SellHouse
          //{
          //    //ItemId = Guid.NewGuid(),
          //    Owner = o,
          //    Address = "dsasd",
          //    BuildYear = 1393,
          //    City = "Isfahan",
          //    Code = "A-512",
          //    Country = "Iran",
          //    Description = "Nothing",
          //    Dimensions = "4X5",
          //    Directions = Directions.North | Directions.South,
          //    Document = "dasd",
          //    Exchange = true,
          //    Facilities = Facilities.Elevator | Facilities.KichenMdfService | Facilities.Parking | Facilities.Warehouse,
          //    Floor = 4,
          //    HasYard = false,
          //    HouseType = HouseType.Apartment,
          //    IssueDate = DateTime.Now,
          //    Loan = 3124,
          //    Meter = 130,
          //    NumberOfBlocks = 4,
          //    NumberOfFloors = 0,
          //    OtherFacilities = "Nothing",
          //    Rooms = 2,
          //    ShareOfSixPart = 4.2f,
          //    State = "Isfahan",
          //    District = "kaveh",
          //    ExchangeDescription = "",
          //    Images = null,
          //    IsRented = false,
          //    Maps = null,
          //    MortgagePayed = 0,
          //    Price = 2222222,
          //    RentAmount = 0,
          //    Substructure = 4,
          //};
  
          GalleryFactory galleryFactory = new GalleryFactory();
  
          Gallery g = new Gallery
          {
              Image = Properties.Resources.jeans_texture03.ToByte()
          };
  
          galleryFactory.SaveGallery(g);
  
          SellHouseFactory sellHouseFactory = new SellHouseFactory();
          //factory.SaveSellHouse(sh);
  
          HashSet&lt;Gallery&gt; galleries = new HashSet&lt;Gallery&gt;();
          galleries.Add(g);
  
          SellHouse sellHouse = sellHouseFactory.GetSellHouses().FirstOrDefault();
          if (sellHouse != null)
          {
              comboBox1.SelectedIndex = (int)sellHouse.Owner.OwnerTitle;
              textBox1.Text = sellHouse.Owner.Firstname+sellHouse.Owner.Lastname;
              //sellHouse.Images = galleries;
  
              sellHouseFactory.SaveSellHouse(sellHouse);
          }
  </code></pre>
  
  <p>i create an object of Owner, then save it, and pass it to sellHouse, and set Images and Maps (which are my many-to-many relation) to null.
  so the sellHouse Created.
  then i was wonder how to add image or map to my sellHouse,
  So i pass a list containing one gallery element to sellHouse Maps property.
  And it generate "Illegal attempt to associate a collection with two open sessions", first i though the reason is because my Gallery didn't saved before i pass it to sell house, so i did as you can see on my code, and did it manually.
  But it still keep on generating that error...</p>
  
  <p>I provide required file so, you can set up an example and understand my code better.</p>
  
  <p>I'm very eager to hear your answer,cause my hands are tied do to my little knowledge over Hibernate and NHibernate.</p>
  
  <p>Sources in DropBox:</p>
  
  <p><a href="https://www.dropbox.com/s/byley3cypho1fqx/ForumQuestion.zip?dl=0" rel="nofollow noreferrer">enter link description here</a></p>