Click here to Skip to main content
13,593,169 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Team,

How to use Thread in Entity Framework?
Here is an example:

I am inserting 10,000 Records into Table

tblProduct objProduct=new tblProduct();
for(int i =0; i<10000; i++)
objProduct=new tblProduct();
objProduct.Name="Product Name Here"+i;

Here Now i want run 5 Process for these 10,000 Records.
ie. I need to insert 5 Records at the same time.
1st process will insert 1 to 200 Records
2nd process will insert 201 to 400 Records
3rd process will insert 301 to 600 Records. etc.

I need to run all the process's at the same time.

Can anybody tell me how is it possible (if thread is the solution then how thread is compatible with my code which is mentioned above.)

Posted 23-Jan-13 1:46am
Updated 23-Jan-13 1:49am
digimanus 23-Jan-13 9:45am
as advocate of the devil please read this about parallel tasks.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

EF seems like the wrong technology here. Presumably you are either initialising a database as a one-off, or performing some kind of export-import to transfer data from another source into your database. Either way, it is better done as a direct SQL script and run against the database without going through EF.
fjdiewornncalwe 23-Jan-13 9:17am
+5. I was thinking the same thing.
sunandandutt 28-Jan-13 2:15am
I think Script is best way to migrate data. but i need to migrate data using LINQ.
i don't want to complete tasks but i need to learn such process to improve my knowledge.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I entirely agree with Solution 1 by BobJanova, but would feel amiss if I didn't offer a little more information here.
With EF there is no reason why you can't parallel this work out provided you aren't dependent on the rows having auto generated primary keys in a specific order because that will not happen with parallel processes. I haven't tried this myself, but as long as you have a separate repository instance for each process they should happily work independently of each other. The caveat here is not to call SaveChanges after every add call because this will just cause you to create deadlocks on your database.
NOTE: I DO NOT SUGGEST YOU DO IT THIS WAY. This is just information as to the potential of being able to multi-thread something like this.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03-2016 | 2.8.180618.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100