How to bulk update entities using EF Core(7)?
I do not want to load entities from the DB server, modify properties and update. I just want to EF generate appropriate UPDATE statement.
Right-click anywhere on the design surface, and select Update Model from Database... In the Update Wizard, select the Refresh tab and select your table then click Finish button.
You can add multiple records or multiple objects using the AddRange method of DbSet as shown in the following code. The code creates a list of department objects and inserts two new departments to the list. We add the list to the context using the AddRange method.
EF Core already supports querying data from stored procedures. This feature will allow mapping the inserts, updates, and deletes generated by SaveChanges to stored procedures in the database.
As the accepted answer pointed, Entity Framework Core doesn't support to updates directly in the database yet.
Disclaimer: I'm the owner of the project Entity Framework Plus
However, EF+ already supports Query Batch Update without loading entities in the context (Support: EF Core, EF6, EF5)
// using Z.EntityFramework.Plus; // Don't forget to include this.
// UPDATE all users inactive for 2 years
ctx.Users.Where(x => x.LastLoginDate < DateTime.Now.AddYears(-2))
.Update(x => new User() { IsSoftDeleted = 1 });
Wiki: Entity Framework Batch Update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With