Beispiel #1
0
        public async Task BulkUpdatePlayersAsync(IEnumerable <Player> players, CancellationToken cancellationToken)
        {
            var factory    = DbProviderFactories.GetFactory(_settings.DataProviderName);
            var connection = factory.CreateConnection();

            connection.ConnectionString = _settings.DbConnectionString;

            try
            {
                await connection.OpenAsync(cancellationToken);

                var context = new DapperPlusContext
                {
                    Connection = connection
                };
                context.Entity <Player>().Table($"{_settings.ObjectsSchema}.{nameof(Player)}s");

                connection.BulkUpdate(context, players);
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    await connection.CloseAsync();
                }
            }
        }
        private async Task RegisterDisciplines(DapperPlusContext dataContext, CancellationToken cancellationToken)
        {
            var disciplines = _disciplines.Select(d => new Discipline {
                Title = d
            });

            dataContext.Entity <Discipline>()
            .Table($"{nameof(Discipline)}s")
            .Identity(x => x.Id);

            await dataContext.BulkActionAsync(x => x.BulkInsert(disciplines), cancellationToken);
        }
        private async Task GenerateStudentsDataLoadAsync(DapperPlusContext dataContext, CancellationToken cancellationToken)
        {
            const int studentCount = 1000;

            var studentsDataFaker = new Faker <Student>()
                                    .RuleFor(u => u.FirstName, f => f.Name.FirstName())
                                    .RuleFor(u => u.LastName, f => f.Name.LastName());

            var students = studentsDataFaker.Generate(studentCount).Distinct(new Student.StudentComparer());

            dataContext.Entity <Student>()
            .Table($"{nameof(Student)}s")
            .Identity(x => x.Id)
            .BatchSize(studentCount);

            await dataContext.BulkActionAsync(x => x.BulkInsert(students), cancellationToken);
        }