I am trying to take all the ids from a table of 1.3 million rows and insert them into another table in another database using Entity Framework. When I do this with SSIS, it is done in under 5 minutes, when I do it using Entity Framework, it takes 3.5 hours.
I have read several sources on slow EF insertion and they seemed to indicate that I should turn off Auto Detect Changes Enabled and that I should avoid using loops I have tried with and without loops and it still takes a very long time.
My code without loops is as follows:
_DB2context.Configuration.AutoDetectChangesEnabled = false;
_DB2context.Table2.AddRange(_DB1context.Table1.Select(m => m.Id)
.Select(pen => new Table2() { Checked = false, Id = pen }));
await _DB2context.SaveChangesAsync();
The third line takes about 3.5 hours to execute. When I tried to do this with loops my code was:
var idList = _DB1context.Table1.Select(m => m.Id).ToList();
int i = 0;
foreach (var id in idList)
{
i++;
_DB2context.Table2.Add(new Table2() {Checked = false, Id = id});
if ((i % 10000)==0)
{
await _DB2context.SaveChangesAsync();
}
}
Is there anything obvious I am doing wrong and is there any way to make this behave more like ssis in running this?