Click here to Skip to main content
11,411,494 members (66,119 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET LINQ
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 23-Jan-13 2:46am
Edited 23-Jan-13 2:49am
v3
Comments
digimanus at 23-Jan-13 9:45am
   
as advocate of the devil please read this about parallel tasks.
Rate this: bad
good
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.
  Permalink  
Comments
Marcus Kramer at 23-Jan-13 9:17am
   
+5. I was thinking the same thing.
sunandandutt at 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
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 223
1 Sergey Alexandrovich Kryukov 195
2 Sascha Lefévre 155
3 ProgramFOX 90
4 Dave Kreskowiak 90
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 7,128
2 Maciej Los 3,440
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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