tronc tronc - 1 month ago 6x
C# Question

Speed up model instantiation for EF6 / sqlite db

I have a large collection of 12000 data entries for example and want to insert them via EF6 into a sqlite database. The most time consumes the instantiation of the data models:

at the moment I loop 12000 times 'new myItem()'

downloaded12000Items.foreach(result =>{
var myItem= new myItem
Id = result.Id,
Description = result.Description,
Property1 = result.Property1


How can I speed up the instantiation of the models or is there maybe another way to insert the data faster into the sqlite database?

EDIT: I have to explain my problem better. The bottleneck is NOT the insert() into the database. To use EF6 .insert(someModel) you have to create an instance of a modelclass of your entity. I have to do this 12000 times, the instantiation of all the 12000 modelclasses takes too much time.

My question was, is there a possibility to fasten up the instatiation process of the model classes, maybe by cloning or something else?

Or, is there maybe a chance to insert the data into the sqlite db without using .insert(someModel), maybe by using a direct sql command or something else? Obviously skipping the model instantiation could be helpful...


The bottleneck is probably the adding of the entities to the context.


At first it doesn't take much time, but after 100s or 1000 records, it does.

See also this answer for other optimizations you might be able to add (read the comments of the linked answer!).