开发者

Linq SubmitChanges() function - when to call?

开发者 https://www.devze.com 2023-01-13 19:35 出处:网络
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 key

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 S开发者_运维技巧ubmitChanges() 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号