Entity Framework is a well-known Microsoft open source (from EF 5) data access technology for .NET applications. Entity Framework is a new framework that completely replaces traditional ADO.NET data access techniques. Entity Framework enables new approaches to work with relational databases. It will reduce the line of code that developers use for data access. This article will briefly describe how to perform batch CRUD operations using the entity framework and its extensions.
To implement Bulk operation, we need to add some base classes.
Customer class
- public class Customer
- {
- [Key]
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public string Country
- {
- get;
- set;
- }
- public bool Status
- {
- get;
- set;
- }
- }
- public class DataContext: DbContext
- {
- public DbSet < Customer > Customers
- {
- get;
- set;
- }
- }
- public static List < Customer > GetCustomers()
- {
- var customers = new List < Customer >
- {
- new Customer()
- {
- Name = "John", Country = "IN", Status = true
- },
- new Customer()
- {
- Name = "Tom", Country = "USA", Status = true
- },
- new Customer()
- {
- Name = "Eric", Country = "USA", Status = false
- },
- new Customer()
- {
- Name = "Sam", Country = "CHINA", Status = true
- },
- new Customer()
- {
- Name = "Rick", Country = "IN", Status = false
- },
- new Customer()
- {
- Name = "Addy", Country = "IN", Status = true
- },
- new Customer()
- {
- Name = "Chang", Country = "CHINA", Status = false
- },
- };
- return customers;
- }
- static void Main(string[] args)
- {
- using(var db = new DataContext())
- {
- // Insert
- var customers = GetCustomers();
- db.Customers.AddRange(customers);
- db.SaveChanges();
- foreach(var customer in db.Customers.ToList())
- {
- Console.WriteLine("CustomerInfo - {0}-{1}-{2}", customer.Name, customer.Country, customer.Status);
- }
- }
- Console.ReadLine();
- }

- static void Main(string[] args)
- {
- using(var db = new DataContext())
- {
- db.Customers.Where(c => c.Country == "USA").Update(c => new Customer()
- {
- Country = "IN"
- });
- foreach(var customer in db.Customers.ToList())
- {
- Console.WriteLine("CustomerInfo - {0}-{1}-{2}", customer.Name, customer.Country, customer.Status);
- }
- }
- Console.ReadLine();
- }

Delete
- static void Main(string[] args)
- {
- using(var db = new DataContext())
- {
- db.Customers.Where(c => c.Country == "CHINA").Delete();
- foreach(var customer in db.Customers.ToList())
- {
- Console.WriteLine("CustomerInfo - {0}-{1}-{2}", customer.Name, customer.Country, customer.Status);
- }
- }
- Console.ReadLine();
- }


Alex SmithPosted Jan 12, 2021, 7:26 PM
When calling UpdateAll, is there a way to get rows affected?
Zack YangPosted Nov 26, 2020, 8:36 PM
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/
Devendra LodhaPosted Jan 11, 2018, 7:21 AM
Hi Bhavik,Just want to ask one question, if the user wants to insert 10000 records would that work using AddRange.
Hamid KhanPosted Apr 18, 2017, 4:36 AM
Very good article thanks we can also use batch operation:https://www.credera.com/blog/technology-insights/microsoft-solutions/entity-framework-batch-operations-using-ef-utilities/
SubashPosted Aug 1, 2016, 4:04 AM
Nice
Hari ShankerPosted Apr 27, 2016, 11:53 PM
Nice
Mohammed IbrahimPosted Apr 5, 2016, 12:23 PM
nice
Rahul Kumar SaxenaPosted Apr 5, 2016, 3:40 AM
Good Show
Humayun Kabir MamunPosted Apr 5, 2016, 2:09 AM
Nice...
Jaipal ReddyPosted Apr 5, 2016, 1:52 AM
good one. .
Manas MohapatraPosted Apr 5, 2016, 12:55 AM
Informative one
Vivek KumarPosted Feb 4, 2016, 2:55 PM
Nice article
Humayun Kabir MamunPosted Dec 27, 2015, 11:10 PM
Nice...
Banketeshvar NarayanPosted Dec 27, 2015, 1:25 PM
Good One