Пример #1
0
        public async Task InsertCustomersSampleData(BikesDBContext db)
        {
            var customers = GetCustomers();

            db.Customers.AddRange(customers);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} customers");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(BikesDBSeeder)}: " + exp.Message);
                throw;
            }

            var products = GetProducts();

            db.Products.AddRange(products);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} products");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(BikesDBSeeder)}: " + exp.Message);
                throw;
            }

            var salePersons = GetSalePersons();

            db.Salespersons.AddRange(salePersons);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} SalePersons");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(BikesDBSeeder)}: " + exp.Message);
                throw;
            }

            var discounts = GetDiscounts();

            db.Discounts.AddRange(discounts);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} Discount");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(BikesDBSeeder)}: " + exp.Message);
                throw;
            }

            var sales = GetSales();

            db.Sales.AddRange(sales);

            try
            {
                int numAffected = await db.SaveChangesAsync();

                _Logger.LogInformation($"Saved {numAffected} Sale");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(BikesDBSeeder)}: " + exp.Message);
                throw;
            }
        }