public async Task <AggregateStats> GetAggregateStats()
        {
            var connection = await _connectionFactory.CreateAsync();

            using (var dbContext = new EntitiesContext(connection, readOnly: true)) // true - set readonly but it is ignored anyway, as this class doesn't call EntitiesContext.SaveChanges()
            {
                var database = dbContext.Database;
                using (var command = database.Connection.CreateCommand())
                {
                    command.CommandText = GetStatisticsSql;
                    database.Connection.Open();
                    using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection | CommandBehavior.SingleRow))
                    {
                        bool hasData = reader.Read();
                        if (!hasData)
                        {
                            return(new AggregateStats());
                        }

                        return(new AggregateStats
                        {
                            Downloads = reader.IsDBNull(0) ? 0 : reader.GetInt32(0),
                            UniquePackages = reader.IsDBNull(1) ? 0 : reader.GetInt32(1),
                            TotalPackages = reader.IsDBNull(2) ? 0 : reader.GetInt32(2)
                        });
                    }
                }
            }
        }
 private static DbConnection CreateConnection(ISqlConnectionFactory connectionFactory)
 {
     return(Task.Run(() => connectionFactory.CreateAsync()).Result);
 }
Esempio n. 3
0
        public async Task <IEntitiesContext> CreateAsync(bool readOnly)
        {
            var sqlConnection = await _connectionFactory.CreateAsync();

            return(new EntitiesContext(sqlConnection, readOnly));
        }