一、更新实体:
EF的DbContext可以实现对一个对象进行更新,而不需要再一次将对象读入内存进行修改,而是采用Attach的方式。
Student stud ; // Get student from DB using (var ctx = new SchoolDBEntities()) { stud = ctx.Students.Where(s => s.StudentName == "New Student1").FirstOrDefault<Student>(); } // change student name in disconnected mode (out of DBContext scope) if (stud != null) { stud.StudentName = "Updated Student1"; } //save modified entity using new DBContext using (var dbCtx = new SchoolDBEntities())
Entity Framework DbContext对一个Entity 进行更新。
最新推荐文章于 2025-01-14 18:33:52 发布