I've to process 1000+ xml files and insert to database tables using EF code first. I'm using Guid as primary key for each table.
Xml file deserialize to One main table and dependent tables (like MyDepTable1, MyDepTable2, MyDepTable3). I am just using below.
dbContext.MyMainTable.Add(myObject);
dbContext.SaveChanges();
Everything works fine if I am using for each loop. All main and dependentables records are inserted.
Once I start using Parallel.ForEach I am getting an exception saying Violation of PRIMARY KEY constraint 'PK_MyDepTable1'. Cannot insert duplicate key in object 'dbo.MyDepTable1'. The duplicate key value is (066c73ce-2dc7-4702-5b0a-08d9ec6f7750).
I've tried using NEWSEQUENTIALID() as Default in SQL, I've tried using Guid.NewGuid() from C#, everything fails when used with Parallel.
Any help is greatly appreciated.
I've referred to stackoverflow articles with similar problems but couldn't find any solution.