Entity Framework core already have
RemoveRange feature which does this thing. Ex of code as shown below:
List<Department> dept = new List<Department>()
{
new Department(){Id=1},
new Department(){Id=2},
new Department(){Id=3}
};
using (var context = new CompanyContext())
{
context.RemoveRange(dept);
await context.SaveChangesAsync();
}
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Entity Framework core already have
RemoveRange feature which does this thing. Ex of code as shown below:
List<Department> dept = new List<Department>()
{
new Department(){Id=1},
new Department(){Id=2},
new Department(){Id=3}
};
using (var context = new CompanyContext())
{
context.RemoveRange(dept);
await context.SaveChangesAsync();
}
EF core will delete them one by one, which means three delete SQL Statements will be executed.
great work. I've been looking for something like this. I rarely see anything about multiple update or deletes in the documentation or tutorials to ef core for some reason.
None
0 Points
3 Posts
Approach to batch delete or update in Entity Framework Core 5
Nov 27, 2020 02:47 AM|zack yang|LINK
I have created a library to batch delete or update records with a round trip on EF Core 5.
Sample code as follows:
await ctx.DeleteRangeAsync(b => b.Price > n || b.AuthorName == "zack yang");
await ctx.BatchUpdate()
.Set(b => b.Price, b => b.Price + 3)
.Set(b=>b.AuthorName,b=>b.Title.Substring(3,2)+b.AuthorName.ToUpper())
.Set(b => b.PubTime, b => DateTime.Now)
.Where(b => b.Id > n || b.AuthorName.StartsWith("Zack"))
.ExecuteAsync();
Github repository: https://github.com/yangzhongke/Zack.EFCore.Batch
Report: https://www.reddit.com/r/dotnetcore/comments/k1esra/how_to_batch_delete_or_update_in_entity_framework/
Participant
1253 Points
931 Posts
Re: Approach to batch delete or update in Entity Framework Core 5
Nov 27, 2020 05:02 AM|yogyogi|LINK
Entity Framework core already have RemoveRange feature which does this thing. Ex of code as shown below:
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
None
0 Points
3 Posts
Re: Approach to batch delete or update in Entity Framework Core 5
Nov 28, 2020 03:28 PM|zack yang|LINK
EF core will delete them one by one, which means three delete SQL Statements will be executed.
In contrast, using my library, only a single SQL
delete from b where id=1 or id=2 or id=3
will be executed.
All-Star
52241 Points
23306 Posts
Re: Approach to batch delete or update in Entity Framework Core 5
Nov 28, 2020 03:49 PM|mgebhard|LINK
Your approach only works if the Ids are contiguous. Seems an unlikely scenario especially after executing the first delete batch.
None
0 Points
3 Posts
Re: Approach to batch delete or update in Entity Framework Core 5
Nov 29, 2020 10:48 AM|zack yang|LINK
You misunderstood my library. It supports all the lambda expression which EF Core supports. So, with it, you can perform any filter condition.
Member
168 Points
201 Posts
Re: Approach to batch delete or update in Entity Framework Core 5
Nov 30, 2020 02:21 PM|bluMarmalade|LINK
great work. I've been looking for something like this. I rarely see anything about multiple update or deletes in the documentation or tutorials to ef core for some reason.