在Entity Framework Core 6 C#中执行SQL批量更新以更新一个属性的方法

huangapple go评论109阅读模式
英文:

How to perform SQL batch updates to update one property in Entity Framework Core 6 C#

问题

以下是您要的翻译:

  1. 我有以下代码,用于使用Entity FrameworkAzure SQL表中执行更新操作
  2. public class DatabaseRepo
  3. {
  4. private readonly UserContext _context;
  5. public DatabaseRepo(UserContext userContext)
  6. {
  7. _context = userContext;
  8. }
  9. public async Task BatchUpdateUsersAsync(List<User> users)
  10. {
  11. foreach (var user in users)
  12. {
  13. var entity = await _context.Users
  14. .FirstOrDefaultAsync(item => item.Id == user.Id);
  15. entity.StartDate = user.StartDate;
  16. await _context.SaveChangesAsync();
  17. }
  18. }
  19. }
  20. 我只想更新所有这些用户的“StartDate”值。是否有更好的批量更新方法?
  21. 我正在使用EF Core 6
  22. 我尝试了这个:
  23. public async Task BatchUpdateUsersAsync(List<User> users)
  24. {
  25. foreach (var user in users)
  26. {
  27. var entry = _context.Set<User>().Add(user);
  28. entry.State = EntityState.Modified;
  29. }
  30. await _context.SaveChangesAsync();
  31. }
  32. 但这会引发关于其他属性不能为空的错误。
英文:

I have the following code to perform an update in Azure SQL table using Entity Framework

  1. public class DatabaseRepo
  2. {
  3. private readonly UserContext _context;
  4. public DatabaseRepo(UserContext userContext)
  5. {
  6. _context = userContext;
  7. }
  8. public async Task BatchUpdateUsersAsync(List<User> users)
  9. {
  10. foreach (var user in users)
  11. {
  12. var entity = await _context.Users
  13. .FirstOrDefaultAsync(item => item.Id == user.Id);
  14. entity.StartDate = user.StartDate;
  15. await _context.SaveChangesAsync();
  16. }
  17. }
  18. }

All I want to do update StartDate value of all these users. Is there a better way to do batch update?

I'm using EF Core 6

I tried this:

  1. public async Task BatchUpdateUsersAsync(List<User> users)
  2. {
  3. foreach (var user in users)
  4. {
  5. var entry = _context.Set<User>().Add(user);
  6. entry.State = EntityState.Modified;
  7. }
  8. await _context.SaveChangesAsync();
  9. }

but this throws an error about some other property can't be null.

答案1

得分: 3

EF Core 7引入了新的批量更新功能,包括更新和删除。
来自Microsoft的信息:ExecuteUpdate和ExecuteDelete

您可以使用以下代码:

  1. var dateTime = DateTime.Now;
  2. _context.Users
  3. .Where(p => p.IsActive || p.Username == "DoNotUpdateMe")
  4. .ExecuteUpdate(p => p.SetProperty(x => x.StartDate, x => dateTime));
  5. _context.SaveChanges();
英文:

EF Core 7 introduces new bulk update features, including update and delete.
Info from Microsoft: ExecuteUpdate and ExecuteDelete

You can use the following:

  1. var dateTime = DateTime.Now;
  2. _context.Users
  3. .Where(p => p.IsActive || p.Username == "DoNotUpdateMe")
  4. .ExecuteUpdate(p => p.SetProperty(x => x.StartDate, x => dateTime));
  5. _context.SaveChanges();

答案2

得分: 0

  1. `Attach` 更新仅需要的列,这些列的 `IsModified` 值为 true,代码如下:
  2. public async Task BatchUpdateUsersAsync(List<User> users)
  3. {
  4. foreach (var user in users)
  5. {
  6. _context.Set<User>().Attach(user);
  7. _OSTContext.Entry(user).Property(x => x.StartDate).IsModified = true; // 仅更新 Startdate 列
  8. }
  9. await _context.SaveChangesAsync();
  10. }
  11. 这将适用于任何 Entity Framework 版本
英文:

Attach Updates only Required column's those are IsModified =true code will be as follows

  1. public async Task BatchUpdateUsersAsync(List&lt;User&gt; users)
  2. {
  3. foreach (var user in users)
  4. {
  5. _context.Set&lt;User&gt;().Attach(user);
  6. _OSTContext.Entry(user).Property(x =&gt; x.StartDate).IsModified = true; //updates only Startdate Column
  7. }
  8. await _context.SaveChangesAsync();
  9. }

This will be use any Entity Frame work version

答案3

得分: -2

你可以使用Select方法进行批量更新,文档中的性能数值如下所示。

更多信息

  1. public class DatabaseRepo
  2. {
  3. private readonly UserContext _context;
  4. public DatabaseRepo(UserContext userContext)
  5. {
  6. _context = userContext;
  7. }
  8. public async Task BatchUpdateUsersAsync(List<User> users)
  9. {
  10. var entities = users.Select(async (user) => await _context.Users.FirstOrDefaultAsync(item => item.Id == user.Id));
  11. entities.ExecuteUpdate(p => p.SetProperty(u => u.StartDate, u => DateTime.Now));
  12. await _context.SaveChangesAsync();
  13. }
  14. }
英文:

You can use the Select method for batch updates, the performance values ​​in the documentation are shared below.

More Info

  1. public class DatabaseRepo
  2. {
  3. private readonly UserContext _context;
  4. public DatabaseRepo(UserContext userContext)
  5. {
  6. _context = userContext;
  7. }
  8. public async Task BatchUpdateUsersAsync(List&lt;User&gt; users)
  9. {
  10. var entities = users.Select(async (user) =&gt; await _context.Users.FirstOrDefaultAsync(item =&gt; item.Id == user.Id));
  11. entities.ExecuteUpdate(p =&gt; p.SetProperty(u =&gt; u.StartDate, u =&gt; DateTime.Now));
  12. await _context.SaveChangesAsync();
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年7月11日 06:05:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定