How do I delete an object from an Entity Framework model without first loading it?

Posted by Tomas Lycken on Stack Overflow See other posts from Stack Overflow or by Tomas Lycken
Published on 2009-02-02T10:38:02Z Indexed on 2010/06/13 22:22 UTC
Read the original article Hit count: 149

I am quite sure I've seen the answer to this question somewhere, but as I couldn't find it with a couple of searches on SO or google, I ask it again anyway...

In Entity Framework, the only way to delete a data object seems to be

MyEntityModel ent = new MyEntityModel();
ent.DeleteObject(theObjectToDelete);
ent.SaveChanges();

However, this approach requires the object to be loaded to, in this case, the Controller first, just to delete it. Is there a way to delete a business object referencing only for instance its ID?

If there is a smarter way using Linq or Lambda expressions, that is fine too. The main objective, though, is to avoid loading data just to delete it.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about entity-framework