Parellel Programming in Net framework 4.0 [Resolved]

Posted by Tushar under C# on 11/26/2010 | Points: 10 | Views : 2880 | Status : [Member] | Replies : 2
Hi all
i am using .NET freamwork 4.0.
and i am also using ADO.Net Entity Data modal for database operation
my question on Parellel Library in Net framework 4.0

i want to save the list of pateint object using ADO.Net Entity Data modal but not using normal loop.
i want to use the Parallel loop that are in .Net framework 4.0

i will give some examples as below



// Using Normal Loop

private void SavePatients(List<Patient> p)
{
using (PatientEntities context = new PatientEntities())
{
for(int i=0; i< p.Count(); i ++)
{
context.AddToPatients(p.ElementAt<Patient>(i));
context.SaveChanges();
}
}
}

//Using Parallel Loop

private void SavePatients(List<Patient> p)
{
using (PatientEntities context = new PatientEntities())
{
Parallel.For(0, p.Count(), i =>
{
context.AddToPatients(p.ElementAt<Patient>(i));
context.SaveChanges();
});
}
}


when i use the normal loop it will perfectly working fine
but when i use the parallel loop the it will give the error as below

The type 'PatientEntities.Data.Entity.Patient' has been mapped more than once.

can anybody pls help me.....

Advance Thanks
Have Nice Time....




Responses

Posted by: Pfarkya on: 12/17/2015 [Member] Starter | Points: 50

Up
0
Down

Resolved
This exception thrown when you use TPL or I can say parallel programming when multiple thread is accessing same object at the same time , to resolve this we have to synchronized this object for the you can SychronizeContext so it will allow you access one thread at a time for the same object.

Tushar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Susanthampy on: 5/20/2011 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi Tushar,

Refer this link,

http://msdn.microsoft.com/en-us/library/dd460693.aspx

Regards,
Susan

Tushar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response