deleting entities using deleteobject() method of objectcontext

Entity framework will not allow you to delete data directly from a database. However, you can pass commands directly to the database with the ExecuteStoreCommand method. Using this method we can send a delete command to the database.

In this article, we will learn how to delete entities using the DeleteObject() method of ObjectContext. The DeleteObject() method takes an EntityObject as a parameter. In this case, EntityObject should be an instance of a Customer.

The below query returns the customer whose name is Vam. The statement
Customer mySelectedCustomer = CustomerQuery.First();
returns the first object in the sequence. When we call the DeleteObject() method, the ObjectContext marks the objects' state to deleted but not delete the object. When we call SaveChanges() method, the context records the deleted state and delete command is constructed and sent to the database.

	var context = new SalesOrderManagementEntities();
	IQueryable CustomerQuery =
               (from p in context.Customer
                where p.Name == "Vam"
                select p);
	Customer mySelectedCustomer =CustomerQuery.First();