NHibernate.LazyInitializationException
Posted: 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.
Then I build a new address record:
Add the new address to the user address book:
I fire up an instance of BasketService and package the basket.
Finally, I set the shipping address for each shipment to the custom address:
When the basket is packaged is when the Warehouse object blows up.
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);
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"];
Code: Select all
_User.Addresses.Add(address);
Code: Select all
IBasketService preCheckoutService = AbleContext.Resolve<IBasketService>();
preCheckoutService.Package(basket, true);
Code: Select all
// UPDATE DESTINATION TO SELECTED ADDRESS AND REDIRECT
foreach (BasketShipment shipment in basket.Shipments)
{
shipment.SetAddress(address);
}