Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core(7) bulk update

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.

like image 884
Skorunka František Avatar asked Jun 22 '16 13:06

Skorunka František


People also ask

How do I update my Entity Framework Core?

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.

How do I insert multiple rows in Entity Framework?

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.

Does EF Core support stored procedures?

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.


1 Answers

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

like image 171
Jonathan Magnan Avatar answered Oct 24 '22 10:10

Jonathan Magnan