NHibernate.LazyInitializationException

For general questions and discussions specific to the AbleCommerce GOLD ASP.Net shopping cart software.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

NHibernate.LazyInitializationException

Post by AbleMods » Wed Apr 24, 2013 2:13 pm

I'm trying to define a custom ship address and package the basket all in one step. But something in nHibernate gets confused and the result is an exception.

What I want to do is add an existing BasketItem object to the basket, package it to generate shipment records, then override the shipping address on the shipment records.

What I'm seeing is a LazyInitializationException, seems to originate from the basketItem.Item.Product.Warehouse.

I must be doing something in the wrong sequence for nHibernate. Like something got disposed without my realizing it. I'm very careful not to fire any .Save() until the very end.

Any thoughts?

First I pull in the basket item I want to add to the basket.

Code: Select all

            //ADD PRODUCT TO BASKET
            User _User = AbleContext.Current.User;
            BasketItem _Item = _BasketItems[0];
            Basket basket = _User.Basket;
            basket.Items.Add(_Item);
Then I build a new address record:

Code: Select all


            // pull in shipping address
            Address address = new Address();
            address.Address1 = _Item.CustomFields["Address1"];
            address.Address2 = _Item.CustomFields["Address2"];
            address.City = _Item.CustomFields["City"];
            address.CountryCode = _Item.CustomFields["Country"];
            address.Email = _Item.CustomFields["Email"];
            address.FirstName = _Item.CustomFields["FirstName"];
            address.LastName = _Item.CustomFields["LastName"];
            address.Phone = _Item.CustomFields["Phone"];
            address.PostalCode = _Item.CustomFields["PostalCode"];
            address.Province = _Item.CustomFields["Province"];
Add the new address to the user address book:

Code: Select all

            _User.Addresses.Add(address);
I fire up an instance of BasketService and package the basket.

Code: Select all

            IBasketService preCheckoutService = AbleContext.Resolve<IBasketService>();
            preCheckoutService.Package(basket, true);
Finally, I set the shipping address for each shipment to the custom address:

Code: Select all

            // UPDATE DESTINATION TO SELECTED ADDRESS AND REDIRECT
            foreach (BasketShipment shipment in basket.Shipments)
            {
                shipment.SetAddress(address);
            }
When the basket is packaged is when the Warehouse object blows up.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

Post Reply