This is a migrated thread and some comments may be shown as answers.

order of deleting operations

1 Answer 44 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Leszek
Top achievements
Rank 1
Leszek asked on 03 Jan 2014, 01:15 PM
Hi,
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;
                dbc.Delete(valuesForDelete);
 
                var listValuesForDelete = from lv in dbc.tableb
                                          where lv.RESOURCE_ID == id
                                          select lv;
                dbc.Delete(listValuesForDelete);
 
                var fieldsForDelete = from f in dbc.tablec
                                      where f.RESOURCE_ID == id
                                      select f;
                dbc.Delete(fieldsForDelete);
 
                var resourceForDelete = from r in dbc.resources
                                        where r.ID == id
                                        select r;
                dbc.Delete(resourceForDelete);
 
                dbc.SaveChanges();

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?
Thanks.
Leszek

1 Answer, 1 is accepted

Sort by
0
Boyan
Telerik team
answered on 07 Jan 2014, 09:10 AM
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.

Regards,
Boyan
Telerik
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!
Tags
General Discussions
Asked by
Leszek
Top achievements
Rank 1
Answers by
Boyan
Telerik team
Share this question
or