Delete Parent Record

Thread is closed for posting
5 posts, 1 answers
  1. Ratndeep
    Ratndeep avatar
    40 posts
    Member since:
    Feb 2009

    Posted 19 Dec 2009 Link to this post

    Hi All,
    I have two tables one is parent and second is child table.when i delete a parent record , i want that corresponding child records sholuld also be deleted for this i have declared one filled of child table foreign key which is primary key of parent table but don't know how to delete parent record with child record.Please help.

  2. Ratndeep
    Ratndeep avatar
    40 posts
    Member since:
    Feb 2009

    Posted 20 Dec 2009 Link to this post

    Can anyone post a solution for above query.I need it.
    Thanks in advance.
  3. Answer
    IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 21 Dec 2009 Link to this post

    Hi Ratndeep,

    You should really take a look at the [Depend] attributte. See the docs here.

    Basically, you apply it to the child list in your parent like:

    public class Order
    {
      [OpenAccess.Depend]
      private IList<OrderLine> orderLines = new List<OrderLine>();
    }

    This will make OA delete the "dependent" objects when the an instance of the Order class is deleted. It works as a "cascading delete" in sql.

    Hope this suits your needs..

    Update:
    Another way to do it if this is a 1:m relationship is to use DeleleOrphans...  See the docs here.


    Regards

    Henrik

  4. Ratndeep
    Ratndeep avatar
    40 posts
    Member since:
    Feb 2009

    Posted 21 Dec 2009 Link to this post

    Thanks Henrik for replying.
    Before posting my query on forum i had looked for it and come to know about cascading delete but was not sure that this is the clean way to do.
  5. IT-Als
    IT-Als avatar
    381 posts
    Member since:
    Sep 2008

    Posted 21 Dec 2009 Link to this post

    Hi Ratndeep,

    So you solved the problem?

    Anyway, the main difference between Depend and Delete Orphans is that the delete orphans is placed on the child and is only deleted when there are no more references to the instance (checked by OA).. Depend doesn't really check this: It just deletes the childs without checking.

    Regards

    Henrik
Back to Top