Reputation: 19356
I have the table A and Table B:
TableA(IDTableA,...) TableB(IDTableB, IDTableA,...)
I know that I can delete on cascada if I set this on the database, but if I haven't defined this in the database and I want to delete the children with EF, which is the best way to do that?
Imagine this situation: user A add to the context the parent, user A add the childs to context, mark the parent and the children as deleted and save the changes. But if an user B add a new child between the time user A loads the children and confirm the changes, the new child added by the user B is not deleted, or I get an exception because of reference integrity.
I try to use a transaction, to set the parent as deleted, then, in the next step, when I make the savechanges
, the parent register is locked, so can't be load by other user. But the problem is the same, other user can make changes in the parent between the parent is load and the save changes.
So my question is, if I want to delete the parent and all its children with EF, which is the best way? The best way is try N times delete the parent? is this a good option when I wnat a good performance?
Thanks. Daimroc.
Upvotes: 0
Views: 142
Reputation: 19356
In this particular case I think that the best way is to use a trigger to delete de childs.
Upvotes: 0
Reputation: 532435
You've answered your own question. Use cascading deletes. You can enable them using EF code first, see How do you ensure Cascade Delete is enabled on a table relationship in EF Code first?, or using the designer by marking the relationship as cascading.
Upvotes: 1