标签:
DbSet in EF 6 has introduced new methods AddRange & RemoveRange. DbSet.AddRange adds collection(IEnumerable) of entities to the DbContext, so you don‘t have to add each entity individually.
IList<Student> newStudents = new List<Student>(); newStudents.Add(new Student() { StudentName = "Student1 by addrange" }); newStudents.Add(new Student() { StudentName = "Student2 by addrange" }); newStudents.Add(new Student() { StudentName = "Student3 by addrange" }); using (var context = new SchoolDBEntities()) { context.Students.AddRange(newStudents); context.SaveChanges(); }
Similarly, DbSet.RemoveRange is used to remove collection of entities from DbSet.
IList<Student> existingStudents = ….. using (var context = new SchoolDBEntities()) { context.Students.RemoveRange(existingStudents); context.SaveChanges(); }
Download sample project for DbSet.AddRange demo.
Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange
标签:
原文地址:http://www.cnblogs.com/purplefox2008/p/5649557.html