Nhibernate引发找到对集合的共享引用

use*_*733 7 nhibernate fluent-nhibernate

我已经阅读了许多与此类似的其他问题 - 但对Nhibernate来说是新手,他们似乎都没有回答我的问题,为什么Inhibernate会抛出"找到对集合的共享引用:Order.ShippingAddress.Items"到以下代码:

 VendorOrderNotificationAcknowledgement ICheckoutVendorService.SendOrderNotification(VendorOrderNotification request)
{
        OrderRepository repo = new OrderRepository();
        var order =(AbstractOrder) repo.FindByCartId(request.OrderNotification.CartOrderId);

        ShippingAddress billingAddress = order.ShippingAddresses[0];
        var itemsFromDb = billingAddress.Items;
        order.ClearAllShippingAddresses();
        wcfFactory.UpdateOrder(order); //NO ERROR THROWN HERE!
        ShippingAddress shippingAddress = orderHelper.CreateShippingAddress(request.ShippingDetails);
        shippingAddress.Items = itemsFromDb;
        order.AddShippingAddress(shippingAddress);

        order.SourceCode = _sourceCode;
        order.TaxAmount = 0;
        order.GiftCertificateAmount = 0;
        order.Status = StatusCode.Approved;
        order.CreatedAt = request.OrderNotification.OrderTime.Year >2010
            ? request.OrderNotification.OrderTime
            : DateTime.Now;
        order.PurchasedDate= 
                                  request.OrderNotification.OrderTime.Year>2010
            ? request.OrderNotification.OrderTime
            : DateTime.Now;
        order.UpdatedAt = DateTime.Now;

        if (request.OrderNotification.OrderCharges != null)
        {
            order.ShippingAmount = request.OrderNotification.OrderCharges.Shipping;
            order.TaxAmount = request.OrderNotification.OrderCharges.DutyAndTaxes;
        }
        else
        {
            order.ShippingAmount = 0;
            order.TaxAmount = 0;
        }
        order.UseGiftWrap = false;
        order.SourceCode = _sourceCode;
        UpdateEshopWorldOrder(order); // THROWS FOUND SHARED REFERENCES TO A COLLECTION: ORDER.SHIPPINGADDRESS.ITEMS

        var orderDto = orderHelper.CreateOrderDto(billingAddress, orderHelper, order);
        var dtoShippingAddresses = orderHelper.CreateDtoShippingAddresses(order);
        orderDto.ShippingAddresses = dtoShippingAddresses;

        ShippingMethodDto shippingMethodDto = 0;

        var mine = wcfFactory.SendOrder(orderDto);

        //More Code below here ...

}


public OrderDto CreateOrderDto(ShippingAddress billingAddress, OrderHelper orderHelper, AbstractOrder order)
{
    OrderDto orderDto = new OrderDto();
    orderDto.AlternateOrderId = order.Id.ToString();
    orderDto.ConfirmationNumber = order.ConfirmationNumber;
    orderDto.Coupons = new string[0];
    orderDto.DiscountAmount = order.DiscountAmount;
    orderDto.GiftCardAmount = order.GiftCertificateAmount;
    orderDto.PurchaseDate = order.PurchasedDate;
    orderDto.ShippingAmount = order.ShippingAmount;
    orderDto.SourceCode = order.SourceCode;
    orderDto.TaxAmount = order.TaxAmount;
    orderDto.UseGiftWrap = order.UseGiftWrap;
    var customerDto = orderHelper.CreateCustomerDto(billingAddress);
    orderDto.SoldTo = customerDto;
    return orderDto;
}

public void UpdateEshopWorldOrder(AbstractOrder order)
{
    try
    {
        //Session.Update(order);
       // transaction.Commit();
          Session.Flush();
    }
    catch (Exception ex)
    {
       _logger.Debug("order saved failed with an error of " + ex.Message);
       _logger.Error(ex);
       throw;
         }
}
Run Code Online (Sandbox Code Playgroud)

任何见解都表示赞赏.... thnx

Wol*_*ang 10

我认为问题是,您的itemsFromDB集合对象是由引用的shippingAddress,也是由billingAddress.

两个实体都需要自己的集合对象.但是,两个集合都可能包含对相同地址对象的引用.

所以我假设用shippingAddress.Items = itemsFromDb;类似的东西替换

shippingAddress.Items.AddRange(itemsFromDb)

要么

shippingAddress.Items = new List<ShippingAddress>(itemsFromDb) 应该做的伎俩