Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am working on a project that use unit of work and repository pattern with orm EF6.
i want insert a list of object about 800k record into database.
i send every 15k or 10k record to instance of repository and then commit it.
it is too slow. how can i give more performance?
i dont want to instal EF.Extensions.
my problem is about foreach loop. its too slow add to repository.

What I have tried:

foreach (var imei in command.IMEIItems) {

var newIMEI = new IMEI(imei.IMEINumber, command.PromotionId);
_imeiRepository.Add(newIMEI);
}

_unitOfWork.Commit();
Posted
Updated 2-May-19 2:51am
v2

You can't really improve the performance using plain vanilla EF. No matter what you do, EF is always going to do one INSERT at a time for each item you're adding. There's no way to speed that up.

I would suggest stepping outside EF for this and using the SqlBulkCopy[^] class instead. You're going to have to do a bit more work to setup this operation, but it'll definitely be a lot faster than EF.
 
Share this answer
 
What Dave said is kind of true and for real big data import worth a try. I just wanted to notice that I offer an AddRange method on my Repository-Impl and do DataContext.Save only once - so for normal use cases a practical way - Btw. Never look at EF Performance in Debug, without Pre-JIT or if EF-Views are not allready generated (you can pre-generated these too). So Always Build Release - run and then run again an do your performance meassurements.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900