c# - 如何在 EF Core 中进行批量更新?

标签 c# .net entity-framework entity-framework-core

我正在尝试使用 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/

相关文章:

c# - MatrixTransform 的矩阵在我看来是转置的

c# - 如何使用 EF+Web API 处理深层对象?

c# - 创建具有多个 OR 和 AND 条件的 LINQ 表达式树

c# - 无法加载 .ps1,因为在此系统上禁用了脚本的执行

c# - 为一个消费者使用 EasyNetQ 多个处理程序不起作用

c# - 在 Azure 服务总线中发布到一个主题并订阅另一个主题的集成测试不可靠是否存在竞争条件?

c# - 如何在 Ubuntu 上的 MonoDevelop 中使用 System.Windows.Forms?

c# - AutoMapper AutoMapper.AutoMapperMappingException : Error mapping types

c# - 在 .NET 中将数组转换为 HashSet<T>

c# - C#禁用Webbrowser声音/应用程序声音