using(var context = new MyDbContext()){ var employees = new List{ new Employee { FirstName = "John", LastName = "Doe" }, new Employee { FirstName = "Jane", LastName = "Doe" }, }; context.AddRange(employees); context.SaveChanges(); }
using(var context = new MyDbContext()){ var employee1 = new Employee { FirstName = "John", LastName = "Doe" }; var employee2 = new Employee { FirstName = "Jane", LastName = "Doe" }; context.AddRange(employee1, employee2); context.SaveChanges(); }In this example, two Employee objects are created and then added to the database using the AddRange method. Package Library: Microsoft.EntityFrameworkCore Overall, the DBContext AddRange method is a useful tool for adding multiple entities to the database at once, reducing the number of database queries needed and improving performance of your application.