static void MigrateTable <T>(Stopwatch sw, Expression <Func <T, object> > getIdExpression, ref long rows, ref long totalTime) { using (var session = ModelEntities.OpenPostgreSQLSession()) { var entitiesCount = session.Query <T>().Count(); var rowsPerCycle = 200000; var numOfCycles = (int)Math.Ceiling((double)entitiesCount / rowsPerCycle); for (var i = 0; i < numOfCycles; i++) { var limitFrom = i * rowsPerCycle; MigrateTablePartial(sw, getIdExpression, ref rows, ref totalTime, session, limitFrom, rowsPerCycle); session.Clear(); GC.Collect(); } } }