Entityframework 6.0 has introduced two new methods for Dbset: AddRange andRemoveRange. DbSet.AddRange adds the collection of entities to the DbContext, sowe don't have to add each entity individually.
Example
- using (SQLLogging.Model.Model context = new SQLLogging.Model.Model())
- {
- List<Employee> employees = new List<Employee>();
- employees.Add(new Employee { Code = "PPP", Name = "Test - P" });
- employees.Add(new Employee { Code = "QQQ", Name = "Test - Q" });
- employees.Add(new Employee { Code = "RRR", Name = "Test - R" });
-
- context.Employees.AddRange(employees);
- context.SaveChanges();
- }
Similarly,DbSet.RemoveRange is used to remove collection of entities from DbSet.
Example
- using (SQLLogging.Model.Model context = new SQLLogging.Model.Model())
- {
- var employees = context.Employees.Where(p => p.Id >= 4).ToList();
- context.Employees.RemoveRange(employees);
-
- context.SaveChanges();
- }