Since you do not have the entity you will need you will effectively be updating a detached entity which is a technique in itself. The trick here is that we are making a fake entity that shares the Entity Key (EF version of a primary key) with the one that needs deleting. Then we mark it as deleted and SaveChanges.
public void DeleteByID(object id) { //make fake entity and set the ID to the id passed in var car = new Car(); car.ID = id; using (var context = new MyDataContext()) { //add this entity to object set context.CreateObjectSet<Car>().Attach(car); //mark it as deleted context.ObjectStateManager.ChangeObjectState(car, EntityState.Deleted); //save changed - effectively saying delete the entity with this ID context.SaveChanges(); } }
It is worth noting that I am following my own twist on the Repository pattern for data access so I have an "EntityRepository" that manages everything EF related. This is where I will make my generic "DeleteByID" method. My EntityRepository uses generics to specify the type of entities that are dealt with so this has to be completely reusable by any entity set (TEntity) or object context (TContext).
How to Delete by ID in a generic way
public void DeleteByID(object id) { var item = new TEntity(); var itemType = item.GetType(); using (var context = new TContext()) { //get the entity container - e.g. MyDataContext - this will have details of all its entities var entityContainer = context.MetadataWorkspace.GetEntityContainer(context.DefaultContainerName, DataSpace.CSpace); //get the name of the entity set that the type T belongs to - e.g. "Cars" var entitySetName = entityContainer.BaseEntitySets.First(b => b.ElementType.Name == itemType.Name).Name; //finding the entity key of this entity - e.g. Car.ID var primaryKey = context.CreateEntityKey(entitySetName, item).EntityKeyValues[0]; //using Reflection to get and then set the property that acts as the entity's key itemType.GetProperty(primaryKey.Key).SetValue(item, id, null); //add this entity to object set context.CreateObjectSet<TEntity>().Attach(item); //mark it as deleted context.ObjectStateManager.ChangeObjectState(item, System.Data.EntityState.Deleted); //save changed - effectively saying delete the entity with this ID context.SaveChanges(); } }
These both translate to the following SQL:
delete from [dbo].[Cars] where ([ID] = 123)And there it is - a reusable delete by ID function with no SELECT!