Deleting the Record

Deleting the record is a bit tricky as it involves setting the state directly. In the following method, first we get the object and set the state of the object to Deleted. Using db.Delete method instead of setting a state would imply the record is deleted immediately
from the database. However, this is not the case. The actual deletion will be pending until we call SaveChanges. Once we call the SaveChanges method, it will generate the delete query for the object and execute it, which in turn will eventually delete the record in the database:


Go to https://goo.gl/QgqKCM access the code.
static void DeleteEmployee()
{
using (var db = new EmployeeDbContext())
{
Employee employeeToBeDeleted =
db.Employees.Where(emp => emp.EmployeeId ==1).FirstOrDefault();
if (employeeToBeDeleted != null)
...
...
}
}

Make changes in the Main method and ensure it looks like this:

static void Main(string[] args)
{
DeleteEmployee();
}