order of deleting operations

Thread is closed for posting
2 posts, 0 answers
  1. Leszek
    Leszek avatar
    17 posts
    Member since:
    Sep 2009

    Posted 03 Jan 2014 Link to this post

    I need delete records from few related tables:
    //delete all related fields
                    var valuesForDelete = from v in dbc.tablea
                                          where v.resource_id== id
                                          select v;
                    var listValuesForDelete = from lv in dbc.tableb
                                              where lv.RESOURCE_ID == id
                                              select lv;
                    var fieldsForDelete = from f in dbc.tablec
                                          where f.RESOURCE_ID == id
                                          select f;
                    var resourceForDelete = from r in dbc.resources
                                            where r.ID == id
                                            select r;

    should be deleted all child records from tablea, then tableb then  tablec and at the end should be deleted resource.
    unfortunately order of deleting is diferent and I have error message that record can't be deleted becouse related child records are exist.
    Is it possible to have control about order of deleting operations?
  2. Boyan
    Boyan avatar
    100 posts

    Posted 07 Jan 2014 Link to this post

    Hello Leszek,

    In fact, the exact order in which the Delete method calls are made is not relevant, as the sequence in which the delete statements will be executed is calculated when the SaveChanges method is called. This means that your approach should be a valid one.

    The issue you described is known to occur in one of the following scenarios:
    1. The tables (tablea, tableb, tablec or resources) could depend on other tables that are not set to be deleted. 
    2. In the resources table, the RESOURCE_ID is not a foreign key and tablea, tableb or tablec is not associated with the resources table.

    If this does not turn to be the case it would be very helpful if you could provide us with the Telerik OpenAccess ORM model of your database. I will convert your ticket from forum post to product feedback enabling you to upload a sample project.

    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top