Deleting entity with child relationships using .Net Entity Framework problem

Posted by Am on Stack Overflow See other posts from Stack Overflow or by Am
Published on 2010-04-10T15:20:09Z Indexed on 2010/04/10 15:23 UTC
Read the original article Hit count: 294

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about .NET