What is the right way to delete all of the collection items of an EF entity? In the code below, DocumentItems is the collection of related document items for a document. Thi
Just to answer to Nix comment to the answer,
it seems to me that the EntityCollection.Remove()
method only marks for deletion the relationships and not the entities, just as the EntityCollection.Clear()
method does.
I know that documentation says that also the entity will be marked for deletion but in my test I've got the behavior I described (anyone can explain me why?).
So, if you have a one to many foreign key constraint in your conceptual model, you cannot save the changes to the context in the persistence store.
The only way I found (since I don't want to CascadeDelete
) is looping through the children and invoke context.DeleteObject
on each of them, thus removing the entity and the associated relationship.