Fastest way to bulk insert records in Entity Framework
Hello everyone! I'm a beginner C# developer and I've encountered a performance issue when bulk inserting data via Entity Framework. I have an application that receives about 10,000 records from an external API every minute and saves them to a SQL Server database.
\n\nCurrently I'm using a regular loop with Add() and SaveChanges() for each record, but this takes about 3-4 minutes, which is critical for my application. Here's an example of my code:
public void SaveRecords(List<MyEntity> records)\n{\n using (var context = new MyDbContext())\n {\n foreach (var record in records)\n {\n context.MyEntities.Add(record);\n context.SaveChanges(); // Calling for each record\n }\n }\n}\n No answers yet.
Log in to leave an answer