How to Bulk Update records in Entity Framework?

前端 未结 7 2171
遥遥无期
遥遥无期 2020-12-01 08:00

I am trying to bulk update records using Entity Framework. I have tried Entity Framework.Extensions Update method.

The Update method is abl

7条回答
  •  抹茶落季
    2020-12-01 08:32

    a) EFCore.BulkExtensions - BatchUpdateAsync

    _dbContext.Set().BatchUpdateAsync( x => new MyObjectEntity{ Id=123, Quantity=100 });
    

    https://github.com/borisdj/EFCore.BulkExtensions

    "EntityFrameworkCore extensions: Bulk operations (Insert, Update, Delete, Read, Upsert, Sync) and Batch (Delete, Update). Library is Lightweight and very Efficient, having all mostly used CRUD operation. Was selected in top 20 EF Core Extensions recommended by Microsoft."

    b) Or EF Extensions - UpdateFromQuery

    _dbContext.Set().UpdateFromQuery( x => new MyObjectEntity{ Id=123, Quantity=100 });
    

    Resource:

    https://entityframework-extensions.net/update-from-query

    https://stackoverflow.com/a/63460251/12425844

    Why UpdateFromQuery is faster than SaveChanges, BulkSaveChanges, and BulkUpdate?

    UpdateFromQuery executes a statement directly in SQL such as UPDATE [TableName] SET [SetColumnsAndValues] WHERE [Key].

    Other operations normally require one or multiple database round-trips which makes the performance slower.

提交回复
热议问题