private static void InsertUsingBulkInsert(int numberOfRows, bool useLinq) { var watch = Stopwatch.StartNew(); using (var dbct = new DemoDbContext(_connectionString)) { var rows = new List <Row>(); var compositeKeyRows = new List <CompositeKeyRow>(); for (int i = 0; i < numberOfRows; i++) { rows.Add(new Row { Column1 = i, Column2 = "" + i, Column3 = DateTime.Now }); compositeKeyRows.Add(new CompositeKeyRow { Id1 = i, Id2 = i, Column1 = i, Column2 = "" + i, Column3 = DateTime.Now }); } if (useLinq) { dbct.BulkInsert(rows, "Rows", row => new { row.Column1, row.Column2, row.Column3 }); dbct.BulkInsert(compositeKeyRows, "CompositeKeyRows", row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 }); } else { dbct.BulkInsert(rows, "Rows", "Column1", "Column2", "Column3"); dbct.BulkInsert(compositeKeyRows, "CompositeKeyRows", "Id1", "Id2", "Column1", "Column2", "Column3"); } } watch.Stop(); Console.WriteLine(watch.Elapsed); }
private static void InsertUsingBulkInsert(int numberOfRows, bool useLinq, bool omitTableName) { var watch = Stopwatch.StartNew(); using (var dbct = new DemoDbContext(_connectionString)) { var rows = new List <Row>(); var compositeKeyRows = new List <CompositeKeyRow>(); for (int i = 0; i < numberOfRows; i++) { rows.Add(new Row { Column1 = i, Column2 = "" + i, Column3 = DateTime.Now }); compositeKeyRows.Add(new CompositeKeyRow { Id1 = i, Id2 = i, Column1 = i, Column2 = "" + i, Column3 = DateTime.Now }); } if (useLinq) { if (omitTableName) { dbct.BulkInsert(rows, row => new { row.Column1, row.Column2, row.Column3 }, row => row.Id); dbct.BulkInsert(compositeKeyRows, row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 }); } else { dbct.BulkInsert(rows, "Rows", row => new { row.Column1, row.Column2, row.Column3 }, row => row.Id); dbct.BulkInsert(compositeKeyRows, "CompositeKeyRows", row => new { row.Id1, row.Id2, row.Column1, row.Column2, row.Column3 }); } } else { if (omitTableName) { dbct.BulkInsert(rows, new string[] { "Column1", "Column2", "Column3" }); dbct.BulkInsert(rows.Take(1000), typeof(Row).GetDbColumnNames("Id")); dbct.BulkInsert(compositeKeyRows, new string[] { "Id1", "Id2", "Column1", "Column2", "Column3" }); } else { dbct.BulkInsert(rows, "Rows", new string[] { "Column1", "Column2", "Column3" }); dbct.BulkInsert(rows.Take(1000), "Rows", typeof(Row).GetDbColumnNames("Id")); dbct.BulkInsert(compositeKeyRows, "CompositeKeyRows", new string[] { "Id1", "Id2", "Column1", "Column2", "Column3" }); } } } watch.Stop(); Console.WriteLine(watch.Elapsed); }