C# 如何在EF Core中进行批量更新?
我正在尝试使用EF Core进行批量更新 如何在EF Core中为此处显示的SQL语句编写代码C# 如何在EF Core中进行批量更新?,c#,.net,entity-framework,entity-framework-core,C#,.net,Entity Framework,Entity Framework Core,我正在尝试使用EF Core进行批量更新 如何在EF Core中为此处显示的SQL语句编写代码 Update Students Set RollNum = 1, Email = 'hsdjasjhd@gmail.com', FirstName = 'Ramamkagja' Where Id in (428, 442, 444, 445, 448, 450, 458, 460) 您可以使用DbContext.UpdateRange方法 范例 using (YourDbCon
Update Students
Set RollNum = 1,
Email = 'hsdjasjhd@gmail.com',
FirstName = 'Ramamkagja'
Where Id in (428, 442, 444, 445, 448, 450, 458, 460)
您可以使用DbContext.UpdateRange方法 范例
using (YourDbContext context = new YourDbContext())
{
List<Student> students = new List<Student>()
{
new Student(){Id=428, Email="hsdjasjhd@gmail.com"},
new Student(){Id=442, Email="hsdjasjhd@gmail.com"},
new Student(){Id=444, Email="hsdjasjhd@gmail.com"}
// Add Full Properties and remaining students here
};
context.Students.UpdateRange(students);
context.SaveChanges();
}
使用(YourDbContext=newyourdbcontext())
{
学生名单=新名单()
{
新学生(){Id=428,电子邮件=”hsdjasjhd@gmail.com"},
新学生(){Id=442,电子邮件=”hsdjasjhd@gmail.com"},
新学生(){Id=444,电子邮件=”hsdjasjhd@gmail.com"}
//在此处添加完整属性和剩余学生
};
上下文。学生。更新(学生);
SaveChanges();
}
确保学生ID存在于DB中,您可以使用DbContext.UpdateRange方法 范例
using (YourDbContext context = new YourDbContext())
{
List<Student> students = new List<Student>()
{
new Student(){Id=428, Email="hsdjasjhd@gmail.com"},
new Student(){Id=442, Email="hsdjasjhd@gmail.com"},
new Student(){Id=444, Email="hsdjasjhd@gmail.com"}
// Add Full Properties and remaining students here
};
context.Students.UpdateRange(students);
context.SaveChanges();
}
使用(YourDbContext=newyourdbcontext())
{
学生名单=新名单()
{
新学生(){Id=428,电子邮件=”hsdjasjhd@gmail.com"},
新学生(){Id=442,电子邮件=”hsdjasjhd@gmail.com"},
新学生(){Id=444,电子邮件=”hsdjasjhd@gmail.com"}
//在此处添加完整属性和剩余学生
};
上下文。学生。更新(学生);
SaveChanges();
}
确保DB中存在学生ID,您可以有多个选项来更新实体框架核心中的批量。 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();
}
参考资料:
您可以有多个选项来更新实体框架核心中的批量。 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();
}
参考资料:
免责声明:我是项目的所有者 您正在寻找“批量更新”功能: 例如:
var ids = new List<int>() { 428, 442, 444, 445, 448, 450, 458, 460 };
ctx.Students.Where(x => ids.Contains(x.Id))
.Update(x => new Student() { Email = "hsdjasjhd@gmail.com", FirstName = "Ramamkagja" });
varids=newlist(){428、442、444、445、448、450、458、460};
其中(x=>ids.Contains(x.Id))
.Update(x=>newstudent(){Email='hsdjasjhd@gmail.com,FirstName=“Ramamkagja”});
一切都将在数据库端执行,因此您不必在您的上下文中加载现有的学生。免责声明:我是项目的所有者 您正在寻找“批量更新”功能: 例如:
var ids = new List<int>() { 428, 442, 444, 445, 448, 450, 458, 460 };
ctx.Students.Where(x => ids.Contains(x.Id))
.Update(x => new Student() { Email = "hsdjasjhd@gmail.com", FirstName = "Ramamkagja" });
varids=newlist(){428、442、444、445、448、450、458、460};
其中(x=>ids.Contains(x.Id))
.Update(x=>newstudent(){Email='hsdjasjhd@gmail.com,FirstName=“Ramamkagja”});
一切都将在数据库端执行,因此您不必在上下文中加载现有的学生