Ack! I am new to Entity Framework and trying to find the easiest way to remove an item.
I have a list with a data source set for TagCategory objects from a database. This is working fine. Now I want to delete the selected item. So I am doing something like this:
TagCategory category = (TagCategory)lstCategories.SelectedItem; using (MyEntities context = new MyEntities()) { context.AttachTo("TagCategories", category); context.DeleteObject(category); context.SaveChanges(); }
It seems straightforward enough, but it will not work. Nothing deleted, no error message, nothing.
So, I see that I can do something like this instead:
using (MyEntities context = new MyEntities()) { string cmd = String.Format("DELETE FROM TagCategory WHERE TagCatID=@ID ", category.TagCatID)); context.ExecuteStoreCommand(qry); }
It seems to work. So am I just going with what works, or can Entity Framework 4 do it?
EDIT: Nevermind. In fact, I had another problem that prevented the execution of the code form. Both published snippets seem to work fine. My apologies.
source share