Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 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;
objDataContaxt.tblProduct.InsertOnSubmit(objProduct);
objDataContaxt.SubmitChanges();
}


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.)

Thanks
Posted
Updated 23-Jan-13 1:49am
v3
Comments
Herman<T>.Instance 23-Jan-13 9:45am    
as advocate of the devil please read this about parallel tasks.

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.
 
Share this answer
 
Comments
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.

Thanks
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.
 
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