.Net Entity Framework Deleting Objects in N-Tier Environment
Posted: November 25, 2008 Filed under: .Net Entity Framework 3.5 SP1 Leave a comment »Well, here I am again, This has turned into a mini series. Please see Adding Objects and Updating Objects for the other episodes of fun!
Deleting objects in an N-Tier environment has been a pleasure and worked as expected. Here we go!
WCF Methods:
/*Private method so I do not have to duplicate these lines everytime*/
private void Delete(EntityObject o)
{
using(CorporateEntities ce = new CorporateEntities())
{
ce.Attach(o);
ce.DeleteObject(o);
ce.SaveChanges();
}
}
/*The exposed method to delete a Store from the database*/
public void RemoveStore(Stores removeStore)
{
this.Delete(removeStore);
}
Nice eh ..? finally something simple and meaningful.
Now for the client code:
client.RemoveStore(client.GetStoreByID(666)); /*WCF Method to retrieve the store I want to delete*/
That is it!!! Not much left to say on deleting. We now have completed this mini series on Adding, Updating and Deleting Objects in N-Tier environment using the entity framework. I will post updates on the posts made when I find better ways to do these operations, but for now this is what works.
Hopes this helps people and avoids the struggles I had. Please post links to better solutions and/or in the comments.
Stay Cool