Saturday, May 22, 2010

Deleting a detached entity with EF4

As unconventional as it may have seemed, it was the reality. With EF1 you had to first query the database to get the entity you wanted to delete. Talk about a waste of time, but the ObjectContext needs to be aware of the entity somehow. In my previous post I talked about how to update a detached entity with EF4. Now deleting a detached entity is just as easy.


void DeleteBook(Book book)
{
using(var ctx = new BookEntities())
{
ctx.Books.Attach(book);
ctx.DeleteObject(book);
ctx.SaveChanges();
}
}

Again pretty easy and should improve on performance.

No comments: