Cannot delete Child from database

Cannot delete Child from database

Old forum URL: forums.lhotka.net/forums/t/9177.aspx


lj98 posted on Wednesday, July 07, 2010

CurrentOrder is root BusinessBase<Order>, OrderItems is child collection BusinessListBase< OrderItemList, OrderItem >

I want to delete a OrderItem, but it still sits in database. Pls help.

CurrentOrder.OrderItems.Remove(CurrentOrderItem);

CurrentOrder = CurrentOrder.Save();

I track the code and find the removed orderItem is not included here:

        private static void Update_OrderItem(ref Order item)
        {
            foreach (OrderItem itemToUpdate in item.OrderItems)
            {
                itemToUpdate.OrderNo = item.OrderNo;

                new OrderItemFactory().Update(itemToUpdate, true);
            }
        }

lj98 replied on Wednesday, July 07, 2010

I read the sample and find a solution:

 

        internal void Update(Order order)
        {
            var factory = new OrderItemFactory();

            var deletedlist = GetDeletedList<OrderItem>(order.OrderItems);
            if (deletedlist.Count > 0)
            {
                foreach (var obj in deletedlist)
                {
                    factory.Update(obj);
                }
            }

            foreach (var obj in order.OrderItems)
            {
                factory.Update(obj);
            }
        }

RockfordLhotka replied on Wednesday, July 07, 2010

If you are using a current version of CSLA (3.6 and higher) then you don't need to worry about this sort of thing. The child data portal functionality handles these details for you.

Copyright (c) Marimer LLC