EntityFramework 学习 一 Delete Entity using DBContext in Disconnected Scenario
Student studentToDelete; //1. Get student from DB using (var ctx = new SchoolDBEntities()) { studentToDelete = ctx.Students.Where(s => s.StudentName == "Student1").FirstOrDefault<Student>(); } //Create new context for disconnected scenario using (var newContext = new SchoolDBEntities()) { newContext.Entry(studentToDelete).State = System.Data.Entity.EntityState.Deleted; newContext.SaveChanges(); }