I have 2 millions of "pb" object instances. I want to add it to my database. Because of memory i splitted list of my objects into a collections of lists and in "for" loop I am trying to add 10'000 of my object instances to database per iteration.
Here is my code
for (int i = 0; i < splittedList.Count; i++)
{
using(MyEntities db = new MyEntities()) //DB context
{
List<pb> outputPBs = splittedList[i];
db.pb.AddRange(outputPBs);
db.SaveChanges();
}
}
Unfortunately first iteration add 10'000 instances, but next one add 10'370 instances instead of 10'000. This bug propagate till the last iteration.
I've made a long debug: I add proper list with AddRange, however db.pb.Local contains 10'370 objects after AddRange execution.
This additional 370 objects are instances from the previous iteration.
Could You help me?
Here is a code, which I used to split a list:
public static List<List<object>> Split(List<object> source)
{
return source
.Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / 3)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();
}
edit: completed
I solved an issue. Problem was connected with one to many relation. I created tree data structure, and added root in different context than branches and leafs