Hello, I have problems with NHibernate, first let me say that I am not a native English talker, well, let's point out ...
As I encounter this error, I searched the Internet and saw how many people talked about it, but most of them had a problem with the cascade attribute, and if I want to say it is completely not relevant to my case, or even if it’s me could not get through.
My database is so small but a bit complicated. My model files created with Entity Developer. 
As you can see, there is inheritance, one-to-many and many-to-many relationships,
Since I am new to NHibernate, I write test code, my normal test worked until I add this many-to-many relationship.
So, I am writing this 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<Gallery> galleries = new HashSet<Gallery>(); 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); }
I create an Owner object, then save it and pass it to sellHouse and set the “Images and Cards” (which are my many-to-many relationships) to null. so sellHouse Created. then I was wondering how to add an image or map to my sellHouse, so I pass a list containing one gallery element to sell the Google Maps property. And it generates an “Unlawful attempt to associate a collection with two open sessions,” I first understand that the reason is that my gallery was not saved before I transferred it to sell the house, so I did, as you can see on my code, and did it manually. But he still continues to generate this error ...
I provide the necessary file, so you can customize the example and better understand my code.
I really want to hear your answer because my hands are tied to my little knowledge of Hibernate and NHibernate.
Thank you, Hassan Fagihi.
Sources in DropBox:
enter the link here