views:

17

answers:

2

When manipulating data using Linq, how often does the SubmitChanges() method have to be called?

In my project, there are a few tables related with foreign keys. In the creation SQL, these foreign keys are constrained so a record that is part of a relationship can't be deleted without the dependant records being deleted first.

Therefore, there is a specific order that records within a relation need to be deleted. Will I have to delete each record, and perform a SubmitChanges() function on the database each time, or can I perform all the deletes in one go?

EDIT: My main question is: Do the records get deleted on SubmitChanges() in the same order as you delete them in code?

+1  A: 

You should try call it as little as possible, but there is a known issue with Linq2SQL when you have insert and deletes that is effectively replacing an entity. In those cases, it is best to call it as soon as possible after the delete and subsequent insert.

Here is a similar question I asked a while back.

leppie
So it would be OK to delete all the related records in one go, and the order they are deleted on SubmitChanges() is the same order that I put in the program?
Greg
@Greg: No, the order is determined by the DataContext. Normally, Insert -> Update -> Delete (I am sure you can see the problem).
leppie
A: 

If you need to delete as a batch e.g. a single transaction, you should only call the SubmitChanges method once when you have marked all the objects to be deleted.

James Culshaw