I'm completely new to Entity Framework, so please forgive me if my logic is skewed / this is the way things already work, but I'm working on an application where:
- I store Parent Information in one table with a Primary key =
ParentId
- For each
ParentId
, I store thousands of records in a child table with a one-to-many foreign-key relationship on ParentId.
So, if the information ever changes for a parent (which can happen fairly often), what I would like to do is have my program perform the equivalent of:
DELETE FROM ChildTable WHERE ParentId = 'xx'
Before updating the child table with the new / updated values for the same ParentId.
From what I've seen, the way I would do that is to either:
- Put in an actual SQL command using the
ctx.Database.ExecuteSqlCommand()
kind-of-concept - On some level, actually loop through the children elements and set them to delete before updating the DB context (which seems like it would be greatly inefficient since I'm guessing it will have to have pulled them from the DB in order to do that and all I want to do is just delete them all).
What is the correct way to do this in EF in the most efficient way possible?