top of page

Entity Framework : The Fastest Way to Insert Large Amount of Data - C# & .NET

Entity Framework is very useful when dealing with many data classes. I am finding that when dealing with a large amount of records, Entity Framework is not the fastest solution. There is BulkInsert feature in Entity Framework but I have not seen anything faster than SQL BulkCopy yet. I will explain how to use it using an example below:

The example will be in two different steps. STEP 1: Convert the list to a data table easily STEP 2: Insert the data using SQL BulkCopy function and the data table from Step 1 For this example, I will say we have students variable stored as IEnumerable<Student> type.

 

PRE-REQUISITE:


Have a list of entities to insert ready

 

STEP 1: Convert the list to a data table easily

You can use the following static method to use to convert students to a data table type:


Then you can use this method as below:


Now you have a data table ready for the SQL Bulk Copy.

 

STEP 2: Insert the data using SQL BulkCopy function and the data table from Step 2

Using dtStudents from STEP 1, you can use the code block below to insert data.


SqlBulkCopy is a part of Microsoft.Data.SqlClient namespace, so you must include it.


 

That's it. Enjoy and thank you!

pngegg (11)_result.webp

<Raank:랑크 /> 구독 하기 : Subscribe

감사합니다! : Thanks for submitting!

bottom of page