Entity Framework - SPLessons

Entity Framework Deletion of Single Entity

Home > Lesson > Chapter 14
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Entity Framework Deletion of Single Entity

Entity Framework Deletion of Single Entity

shape Example

Write the following code to delete an entity from the database. [csharp] Employee employeeToDelete; using (var con = new EmployeeDBEntities()) { employeeToDelete = con.Employee.Where(e => e.Name == "Emp1").FirstOrDefault<Student>(); } //Create new context using (var newCon = new EmployeeDBEntities()) { newCon.Entry(employeeToDelete).State = System.Data.Entity.EntityState.Deleted; newCon.SaveChanges(); } [/csharp]