Пример #1
0
        static async Task TestAsync(BulkContext context, NpgsqlBulkUploader uploader, List <Address> data)
        {
            Console.WriteLine("");
            Console.WriteLine("ASYNC version...");
            Console.WriteLine("");


            var sw = Stopwatch.StartNew();
            await uploader.InsertAsync(data);

            sw.Stop();
            Console.WriteLine($"Dynamic solution inserted {data.Count} records for {sw.Elapsed }");
            Trace.Assert(context.Addresses.Count() == data.Count);

            data.ForEach(x => x.HouseNumber += 1);

            sw = Stopwatch.StartNew();
            await uploader.UpdateAsync(data);

            sw.Stop();
            Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed }");

            context.Database.ExecuteSqlCommand("TRUNCATE addresses CASCADE");
            sw = Stopwatch.StartNew();
            await uploader.ImportAsync(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(TransactionScopeAsyncFlowOption.Enabled))
            {
                await uploader.InsertAsync(data);
            }
            Trace.Assert(context.Addresses.Count() == 0);

            sw = Stopwatch.StartNew();
            await uploader.UpdateAsync(data);

            sw.Stop();
            Console.WriteLine($"Dynamic solution updated {data.Count} records for {sw.Elapsed } (after transaction scope)");
        }
Пример #2
0
        public async Task <int> Ingest(IEnumerable <T> source, IngestMode ingestMode, CancellationToken cancel)
        {
            var list  = source.GroupBy(s => pk.PropertyInfo.GetValue(s).ToString()).Select(g => g.First()).ToList();
            var count = list.Count();

            logger.LogInformation($"adding {typeof(T).Name}...");
            var stopwatch = Stopwatch.StartNew();

            var uploader = new NpgsqlBulkUploader(context);
            await uploader.InsertAsync(list);

            logger.LogInformation($"it took {stopwatch.Elapsed} to injest {count} records");
            return(count);
        }
Пример #3
0
 public static async Task BulkInsertAsync <TEntity>(this DbContext dbContext, IEnumerable <TEntity> entities)
     where TEntity : Entity
 {
     var bulk = new NpgsqlBulkUploader(dbContext);
     await bulk.InsertAsync(entities);
 }