我正在尝试使用 EF Core 批量更新。
如何在 EF Core 中为此处显示的 SQL 语句编写代码?
Update Students
Set RollNum = 1,
Email = '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="640c17000e05170e0c00240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>',
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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a02190e000b1900020e2a0d070b030644090507" rel="noreferrer noopener nofollow">[email protected]</a>"},
new Student(){Id=442, Email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aac2d9cec0cbd9c0c2ceeacdc7cbc3c684c9c5c7" rel="noreferrer noopener nofollow">[email protected]</a>"},
new Student(){Id=444, Email="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82eaf1e6e8e3f1e8eae6c2e5efe3ebeeace1edef" rel="noreferrer noopener nofollow">[email protected]</a>"}
// Add Full Properties and remaining students here
};
context.Students.UpdateRange(students);
context.SaveChanges();
}
确保数据库中存在学生 ID
关于c# - 如何在 EF Core 中进行批量更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66364022/