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();
}  

 

posted @ 2017-03-25 21:30  蓝平凡  阅读(201)  评论(0编辑  收藏  举报