Example #1
0
        public async Task <TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default)
        {
            await _dbSet.AddAsync(entity, cancellationToken);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(entity);
        }
Example #2
0
        public async Task SeedAuctionAsync(ILoggerFactory logFactory, int retryTime = 0)
        {
            //Tables to seed: will have 2 slots, 1 auction, 6 bids
            try
            {
                // _context.Slots.RemoveRange(_context.Slots);
                // _context.Categories.RemoveRange(_context.Categories);
                // _context.Traders.RemoveRange(_context.Traders);
                await _context.SaveChangesAsync();

                if (!await _context.Categories.AnyAsync())
                {
                    await _context.Categories.AddRangeAsync(GetConfiguredCategories());

                    await _context.SaveChangesAsync();
                }

                if (!await _context.Traders.AnyAsync())
                {
                    await _context.Traders.AddRangeAsync(GetConfiguredTraders());

                    await _context.SaveChangesAsync();
                }

                if (!await _context.Slots.AnyAsync())
                {
                    await _context.Slots.AddRangeAsync(GetConfiguredSlots());

                    await _context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                if (retryTime < 5)
                {
                    retryTime++;
                    var log = logFactory.CreateLogger <AuctionDbContext>();
                    log.LogError(ex.Message);
                    await SeedAuctionAsync(logFactory, retryTime);
                }
                throw;
            }
        }