您可以有多个选项来更新实体框架核心中的批量。
UpdateRange 从版本 1 开始可用。因此您可以使用该方法更新所有条目,如下所示:
using (dbContext context = new dbContext())
{
var entities = new List<Student>()
{
new Student(){Id=8, Email="test1@gmail.com"},
new Student(){Id=2, Email="test2@gmail.com"},
new Student(){Id=4, Email="test3@gmail.com"}
};
context.Students.UpdateRange(students);
context.SaveChanges();
}
为了获得更好的性能,我更喜欢的第二种方法是使用存储过程/或原始 SQL 查询并使用 efcore 从您的代码中调用它。
如下:
string query = "Set RollNum = 1, Email = 'hsdjasjhd@gmail.com', FirstName = 'Ramamkagja' Where Id in (428, 442, 444, 445, 448, 450, 458, 460);
context.Database.ExecuteSqlCommand(query);
参考资料:
Execute Sql Command
Update Range