static void TestInheritanceCase() { var streets = new[] { "First", "Second", "Third" }; var codes = new[] { "001001", "002002", "003003", "004004" }; var extraNumbers = new int?[] { null, 1, 2, 3, 5, 8, 13, 21, 34 }; var context = new BulkContext("DefaultConnection"); context.Database.ExecuteSqlCommand("DELETE FROM addresses2"); var data = Enumerable.Range(0, 100000) .Select((x, i) => new Address2() { StreetName = streets[i % streets.Length], HouseNumber = i + 1, PostalCode = codes[i % codes.Length], ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], LocalizedName = streets[i % streets.Length], Index1 = i, Index2 = i }).ToList(); var uploader = new NpgsqlBulkUploader(context); context.Database.ExecuteSqlCommand("TRUNCATE addresses CASCADE"); var sw = Stopwatch.StartNew(); uploader.Insert(data); sw.Stop(); Console.WriteLine($"Dynamic solution inserted {data.Count} records for {sw.Elapsed }"); // checking consitency foreach (var addr in data) { if (addr.Index1 != addr.Index2) { Console.WriteLine($"INCONSITENT! Id: {addr.AddressId}/{addr.Address2Id}, {addr.Index1} != {addr.Index2}"); } } data.ForEach(x => x.HouseNumber += 1); sw = Stopwatch.StartNew(); uploader.Update(data); sw.Stop(); Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed }"); }
static void TestPlainCase() { var streets = new[] { "First", "Second", "Third" }; var codes = new[] { "001001", "002002", "003003", "004004" }; var extraNumbers = new int?[] { null, 1, 2, 3, 5, 8, 13, 21, 34 }; var context = new BulkContext("DefaultConnection"); context.Database.ExecuteSqlCommand("TRUNCATE addresses cascade"); var data = Enumerable.Range(0, 100000) .Select((x, i) => new Address() { StreetName = streets[i % streets.Length], HouseNumber = i + 1, PostalCode = codes[i % codes.Length], ExtraHouseNumber = extraNumbers[i % extraNumbers.Length] }).ToList(); var uploader = new NpgsqlBulkUploader(context); context.Database.ExecuteSqlCommand("DELETE FROM addresses"); var sw = Stopwatch.StartNew(); HardcodedInsert(data, context); sw.Stop(); Console.WriteLine($"Hardcoded solution inserted {data.Count} records for {sw.Elapsed }"); context.Database.ExecuteSqlCommand("DELETE FROM addresses"); sw = Stopwatch.StartNew(); uploader.Insert(data); sw.Stop(); Console.WriteLine($"Dynamic solution inserted {data.Count} records for {sw.Elapsed }"); data.ForEach(x => x.HouseNumber += 1); sw = Stopwatch.StartNew(); uploader.Update(data); sw.Stop(); Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed }"); }
static void TestPlainCase() { var streets = new[] { "First", "Second", "Third" }; var codes = new[] { "001001", "002002", "003003", "004004" }; var extraNumbers = new int?[] { null, 1, 2, 3, 5, 8, 13, 21, 34 }; var addressTypes = new AddressType?[] { null, AddressType.Type1, AddressType.Type2 }; var dates = new DateTime?[] { null, DateTime.Now }; var guids = new Guid?[] { null, Guid.Empty }; var decimals = new decimal?[] { null, decimal.Zero }; var context = new BulkContext("DefaultConnection"); context.Database.ExecuteSqlCommand("TRUNCATE addresses cascade"); var data = Enumerable.Range(0, 100000) .Select((x, i) => new Address() { StreetName = streets[i % streets.Length], HouseNumber = i + 1, PostalCode = codes[i % codes.Length], ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], Type = addressTypes[i % addressTypes.Length], Date = dates[i % dates.Length], Guid = guids[i % guids.Length], Dec = decimals[i % decimals.Length] }).ToList(); var uploader = new NpgsqlBulkUploader(context); context.Database.ExecuteSqlCommand("DELETE FROM addresses"); var sw = Stopwatch.StartNew(); HardcodedInsert(data, context); sw.Stop(); Console.WriteLine($"Hardcoded solution inserted {data.Count} records for {sw.Elapsed }"); context.Database.ExecuteSqlCommand("DELETE FROM addresses"); sw = Stopwatch.StartNew(); uploader.Insert(data, InsertConflictAction.UpdateProperty <Address>(x => x.AddressId, x => x.Dec)); uploader.Insert(data, InsertConflictAction.DoNothing()); sw.Stop(); Console.WriteLine($"Dynamic solution inserted {data.Count} records for {sw.Elapsed }"); data.ForEach(x => x.HouseNumber += 1); sw = Stopwatch.StartNew(); uploader.Update(data); sw.Stop(); Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed }"); TestViaInterfaceCase(data, context); context.Database.ExecuteSqlCommand("TRUNCATE addresses CASCADE"); sw = Stopwatch.StartNew(); uploader.Import(data); sw.Stop(); Console.WriteLine($"Dynamic solution imported {data.Count} records for {sw.Elapsed }"); // With transaction context.Database.ExecuteSqlCommand("TRUNCATE addresses CASCADE"); using (var transaction = new TransactionScope()) { uploader.Insert(data); } Trace.Assert(context.Addresses.Count() == 0); sw = Stopwatch.StartNew(); uploader.Update(data); sw.Stop(); Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed } (after transaction scope)"); TestAsync(context, uploader, data).Wait(); }