码迷,mamicode.com
首页 > 数据库 > 详细

Entity Framework 6.0 Tutorials(7):DbSet.AddRange & DbSet.RemoveRange

时间:2016-07-07 12:54:12      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

DbSet.AddRange & DbSet.RemoveRange:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!