locked
Getting entity children in Entity_Deleting RRS feed

  • Question

  • Hi, 

    I'm having this problem and can't find an answer by myself. This is a *very* simple situation: I am deleting an entity that has children from two tables. In entity_Deleting, I'm checking if entity has children. Whatever I do, my code never finds children for the entity. 

    If I do: entity.childTable.Count(), it is always 0; if I try to read children from the entity using a foreach statement, it never finds one. 

    I'm sure I have done this before, but don't know what may happen this time. 

    What am I missing here? 

    Please help. 

    Thanks and bye ... 

    Saturday, May 26, 2012 4:16 PM

Answers

  • Do you use cascade delete option for related tables? If you use cascade delete lightswitch automatically delete related entities before deleting that entity.

    • Proposed as answer by ninoid Monday, May 28, 2012 6:01 AM
    • Marked as answer by Manuel Rivera Monday, May 28, 2012 1:09 PM
    Sunday, May 27, 2012 6:25 AM
  • Alternatively, if you want to stop the user from deleting an item that has children, write code in the CanDelete event of the parent entity to check if there are any children. You could also use the CanExecute event on the Delete button to make this check.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by babloo1436 Sunday, May 27, 2012 11:47 AM
    • Marked as answer by Manuel Rivera Monday, May 28, 2012 1:09 PM
    Sunday, May 27, 2012 11:42 AM
  • Just as an FYI,

    The cascade delete is applied before the save pipeline invokes the validation logic and the pre events for the changed entities.  This is done so that you can get notification and run your business logic against all of the state changes that happen to the entities within the system.  You can read more about the save pipeline here - http://www.code-magazine.com/Article.aspx?quickid=1103071

    • Marked as answer by Manuel Rivera Wednesday, May 30, 2012 7:35 PM
    Wednesday, May 30, 2012 1:16 PM

All replies

  • Do you use cascade delete option for related tables? If you use cascade delete lightswitch automatically delete related entities before deleting that entity.

    • Proposed as answer by ninoid Monday, May 28, 2012 6:01 AM
    • Marked as answer by Manuel Rivera Monday, May 28, 2012 1:09 PM
    Sunday, May 27, 2012 6:25 AM
  • Alternatively, if you want to stop the user from deleting an item that has children, write code in the CanDelete event of the parent entity to check if there are any children. You could also use the CanExecute event on the Delete button to make this check.


    Simon Jones
    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.

    • Proposed as answer by babloo1436 Sunday, May 27, 2012 11:47 AM
    • Marked as answer by Manuel Rivera Monday, May 28, 2012 1:09 PM
    Sunday, May 27, 2012 11:42 AM
  • Hi and thanks for your answers. 

    ninoid: Wow, really didn't know LS deletes the related entities *before* Entity_deleting event. I thought this may happen in Entity_deleted *but* not in Entity_deleting. This is really useful knowledge indeed. I also thought this was a sql server job and that this didn't happen in LS ... very good to have realized this. 

    Simon: yes, CanDelete is * definitely* the way to go here ... 

    Thanks again and bye ... 

    Monday, May 28, 2012 1:15 PM
  • I had some bugs because of cascade delete and entity_deleting so I recognize that you could have similar situation...

    glad to give you idea. 

    all the best...

    Tuesday, May 29, 2012 5:49 AM
  • Just as an FYI,

    The cascade delete is applied before the save pipeline invokes the validation logic and the pre events for the changed entities.  This is done so that you can get notification and run your business logic against all of the state changes that happen to the entities within the system.  You can read more about the save pipeline here - http://www.code-magazine.com/Article.aspx?quickid=1103071

    • Marked as answer by Manuel Rivera Wednesday, May 30, 2012 7:35 PM
    Wednesday, May 30, 2012 1:16 PM
  • Hi snomis, 

    This adds to clarify the whole process. After you thought again, it is a very logic behavior indeed. 

    Thanks for further clarification. This is important for complete understanding of LS basics. 

    Thanks again and bye ... 

    Wednesday, May 30, 2012 7:39 PM