entity framework insert multiple records code example

Example 1: update multiple records with entity framework

using (var dbcontext = new MyModel()){
   var matchedRecords = dbcontext.DummyTable.Where(e => e.code.Equals(entry.code) && e.isValid.Equals(true)).ToList();
   matchedRecords.ForEach(e => e.isValid = false);
   dbcontext.SaveChanges();
}

Example 2: insert multiple records on the Database in Entity Framework Core

var dept1 = new Department() { Name = "Development" };
var dept2 = new Department() { Name = "HR" };
var dept3 = new Department() { Name = "Marketing" };
 
using (var context = new CompanyContext())
{
    context.Department.AddRange(dept1, dept2, dept3);
    context.SaveChanges();
}

Tags:

Misc Example