Last week I had a bug that I tried to recreate. A piece of existing code did a mass insert on the database. Though, a value was not allowed to be duplicate in the database The old code persisted the changes to the database after every AddObject with a SaveChanges. This was not performing very well. So, instead of saving it every time I saved it after every 1000 records (and did this in a transaction,but that is not relevant for this sample).
this code below gives a overview of what I did.
TestEntities test = new TestEntities();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
string q = j.ToString();
if (!test.warehouses.Any(x => x.combi == q))
{
warehouse wh = new warehouse();
wh.combi = j.ToString();
wh.ean = j.ToString();
test.warehouses.AddObject(wh);
}
}
}
test.SaveChanges();
What I did not know was that entity framework only queries the data in the database and not the pending data so the any query gives no result on the database (and I was assuming that there would be a result.) Hence, this results in unwanted duplicates in the code above.
For now I solved it to store all soon to be added objects in memory and then store them in the database. This works, but gives a lot of overhead in the code. Is there a way to work around this issue? Can you tell EF to work on the database and the pending changes? Are all ORM's working like this?
Thanks, Patrick