【问题标题】:NHibernate: Illegal attempt to associate a collection with two open sessionsNHibernate:非法尝试将集合与两个打开的会话相关联
【发布时间】:2015-03-23 14:58:21
【问题描述】:

我在使用 NHibernate 时遇到了问题,首先让我说我的母语不是英语,好吧,让我们开始吧。

当我遇到这个错误时,我在互联网上搜索,看到很多人在谈论它,但他们中的大多数人都有级联属性的问题,如果我想说,完全不符合我的情况,或者即使它我是不是打不通。

我的数据库很小,但有点复杂。 我使用 Entity Developer 生成的模型文件。

如您所见,存在继承、一对多和多对多关系,

由于我是 NHibernate 的新手,所以我编写了一个测试代码,我的正常测试工作正常,直到我添加了这种多对多关系。

所以我写了这段代码:

//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,并将图像和地图(这是我的多对多关系)设置为 null。 所以sellHouse创建了。 然后我想知道如何将图像或地图添加到我的 sellHouse, 因此,我将包含一个画廊元素的列表传递给 sellHouse Maps 属性。 它会生成“非法尝试将收藏与两个打开的会话相关联”,首先我的原因是因为我的画廊在我通过它卖房子之前没有保存,所以我做了你可以在我的代码中看到的,并且做了它手动。 但它仍然继续产生那个错误......

我提供了所需的文件,所以您可以设置一个示例并更好地理解我的代码。

我非常渴望听到你的回答,因为我对 Hibernate 和 NHibernate 知之甚少。

DropBox 中的来源:

enter link description here

【问题讨论】:

    标签: c# nhibernate


    【解决方案1】:

    可能在您的所有工厂方法中,您都会打开一个新会话。 但是您必须在每个线程(或事务)中只打开一个会话。

    【讨论】:

      猜你喜欢
      • 2017-05-01
      • 2010-09-18
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      相关资源
      最近更新 更多