问题描述
我在使用NHibernate时遇到了麻烦,首先让我说我不是母语为英语的人,好吧,让我们指出一下.
I'm having trouble with NHibernate, First let me say I'm not native English talker, OK, let get to point.
当我遇到此错误时,我在互联网上进行搜索,看到很多人在谈论它,但是其中大多数人都具有级联属性的问题,如果我想说的话,完全不符合我的情况,甚至我无法通过.
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.
我的数据库很小,但是有点复杂.我的模型文件是使用Entity Developer生成的.
My Database is so small, but a bit complex.My model files generated using Entity Developer.
如您所见,存在继承关系,一对多关系和多对多关系船,
As you can see there is inheritance, one-to-many and many-to-many relation ships,
由于我是NHibernate的新手,所以我编写了一个测试代码,我的常规测试成功了,直到我添加了这个多对多关系.
As I am new to NHibernate, i write a test code, my normal test worked, till i add this many-to-many relation ship.
所以我写这段代码:
//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);
}
我创建了一个Owner对象,然后保存它,并将其传递给sellHouse,并将Images and Maps(这是我的多对多关系)设置为null.因此,sellHouse已创建.然后我想知道如何在我的SellHouse中添加图片或地图,因此,我将包含一个图库元素的列表传递给sellHouse Maps属性.并生成非法尝试将一个收藏与两个打开的会话相关联",首先,尽管我的原因是因为我的图库没有通过保存才被卖出,所以我按照代码上的说明做了,并且做了手动操作.但是它仍然继续产生该错误...
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...
我提供了必需的文件,因此,您可以设置示例并更好地理解我的代码.
I provide required file so, you can set up an example and understand my code better.
我非常渴望听到您的回答,因为我的双手与我对Hibernate和NHibernate的一点了解联系在一起.
I'm very eager to hear your answer,cause my hands are tied do to my little knowledge over Hibernate and NHibernate.
DropBox中的来源:
Sources in DropBox:
推荐答案
可能在您所有的工厂方法中都打开了一个新会话.但是您只需要在每个线程(或事务)中打开一个会话即可.
Probably in all of your factory method you open a new session.But you have to open just one session in every thread (or transaction).
这篇关于NHibernate:非法尝试将一个集合与两个打开的会话相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!