Best Way to Delete Related Enities

Posts   
 
    
Gianc1412
User
Posts: 40
Joined: 30-Oct-2007
# Posted on: 04-Feb-2010 06:34:30   

I'm using Self Servicing version 2.6

How would I perform a cascading delete of the [Menu Category] object given the schema below

[Menu Category] contains zero or more [Menu Item(s)] [Menu Item] contains zero or more [Menu Price(s)]

I'm trying to do this strictly with the generated code. Below is an example of what I am trying. Not only does it not work, but I think there's got to be a more efficient way.


            MenuPriceCollection prices = new MenuPriceCollection();
            RelationPredicateBucket bucket0 = new RelationPredicateBucket();
            bucket0.Relations.ObeyWeakRelations = true;
            bucket0.Relations.Add(MenuCategoryEntity.Relations.MenuItemEntityUsingCategoryId);
            bucket0.Relations.Add(MenuItemEntity.Relations.MenuPriceEntityUsingItemId);
            bucket0.PredicateExpression.Add(MenuCategoryFields.Id == categoryId);
            prices.DeleteMulti(bucket0.PredicateExpression, bucket0.Relations);

            MenuItemCollection items = new MenuItemCollection();
            RelationPredicateBucket bucket1 = new RelationPredicateBucket();
            bucket1.Relations.ObeyWeakRelations = true;
            bucket1.Relations.Add(MenuCategoryEntity.Relations.MenuItemEntityUsingCategoryId);
            bucket1.PredicateExpression.Add(MenuCategoryFields.Id == categoryId);
            items.DeleteMulti(bucket1.PredicateExpression, bucket0.Relations);

            MenuCategoryCollection categories = new MenuCategoryCollection();
            IPredicateExpression deleteFilter = new PredicateExpression(MenuCategoryFields.Id == categoryId);
            categories.DeleteMulti(deleteFilter);

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-Feb-2010 09:45:21   

Why don't use database cascade deletes?

Gianc1412
User
Posts: 40
Joined: 30-Oct-2007
# Posted on: 04-Feb-2010 16:23:46   

I will look into it for efficiency, but, is there a way to delete with relations using LLBLGEN

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 04-Feb-2010 21:23:32   

When you say it doesn't work, what do you mean exactly ? You get an exception, or just not the results you were expecting.

As far as efficiency goes, the way you are doing it is pretty much the most efficient way, as it deletes the entities directly from the database without loading them first. Any other method is going to involve loading the entity collections first and then deleting them.

Matt

Gianc1412
User
Posts: 40
Joined: 30-Oct-2007
# Posted on: 05-Feb-2010 04:14:41   

Yeah, the DeleteMulti methods are returning zero.

Ultimately, we just enabled CASCADE on the database tables.

Thanks for the info.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Feb-2010 04:50:58   

Gianc1412 wrote:

Yeah, the DeleteMulti methods are returning zero.

That should work though. Do you see any generated sql when do such DeleteMulti's ?

David Elizondo | LLBLGen Support Team