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, 2:26 PM EST
When calling UpdateAll, is there a way to get rows affected?
Zack YangPosted Nov 26, 2020, 3:36 PM EST
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, 2:21 AM EST
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, 12:36 AM EDT
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, 12:04 AM EDT
Nice
Hari ShankerPosted Apr 27, 2016, 7:53 PM EDT
Nice
Mohammed IbrahimPosted Apr 5, 2016, 8:23 AM EDT
nice
Rahul Kumar SaxenaTOP 100Posted Apr 4, 2016, 11:40 PM EDT
Good Show
Humayun Kabir MamunPosted Apr 4, 2016, 10:09 PM EDT
Nice...
Jaipal ReddyPosted Apr 4, 2016, 9:52 PM EDT
good one. .
Manas MohapatraPosted Apr 4, 2016, 8:55 PM EDT
Informative one
Vivek KumarPosted Feb 4, 2016, 9:55 AM EST
Nice article
Humayun Kabir MamunPosted Dec 27, 2015, 6:10 PM EST
Nice...
Banketeshvar NarayanPosted Dec 27, 2015, 8:25 AM EST
Good One