原文:http://www.entityframeworktutorial.net/entityframework6/addrange-removerange.aspx
EF 6中的DbSet引入了新的方法AddRange&RemoveRange。
DbSet.AddRange将实体的集合(IEnumerable)添加到DbContext,不必单独添加每个实体。
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();
}
类似的,DbSet.RemoveRange用于从DbSet中删除实体的集合。
IList<Student> existingStudents = ….. using (var context = new SchoolDBEntities())
{
context.Students.RemoveRange(existingStudents);
context.SaveChanges();
}