Deleting entity with child relationships using .Net Entity Framework problem
- by Am
My God, EF is so frustrating. I can't seem to be able to get my head around what I need to do so I can delete an object. I seem to be able to remove the object but not the related child objects. Can anyone tell me what is the rule of thumb when you want to delete all related child objects of a given object?
I've tried loading all related objects like this:
if (!object.childobjects.IsLoaded)
object.childobjects.Load();
but once I do the following I get errors related to the relationships:
modelcontext.DeleteObject(object);
modelcontext.SaveChanges();
Why can't I just load the object using modelcontext.GetObjectByKey and remove it along with its child objects?
My other question is can I delete an object using Entity command like so?
DELETE e from objectset as e where e.id = 12
I've tried few variations and all of them throw exceptions.