public async Task <Customer> InsertCustomerAsync(Customer customer)
        {
            _Context.Add(customer);
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (System.Exception exp)
            {
                _Logger.LogError($"Error in {nameof(InsertCustomerAsync)}: " + exp.Message);
            }

            return(customer);
        }
Пример #2
0
        public async Task InsertCustomersSampleData(CustomersDbContext db)
        {
            var httpClient   = new HttpClient();
            var statesString = await httpClient.GetStringAsync("http://lookup.api/api/v1/lookup/states");

            var states = JsonSerializer.Deserialize <List <State> >(statesString,
                                                                    new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });
            var customers = GetCustomers(states);

            db.Customers.AddRange(customers);

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

                _Logger.LogInformation($"Saved {numAffected} customers");
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
                throw;
            }
        }
Пример #3
0
        public async Task InsertCustomersSampleData(CustomersDbContext db)
        {
            try
            {
                var httpClient   = _HttpClientFactory.CreateClient("lookup-api");
                var statesString = await httpClient.GetStringAsync("states");

                if (!String.IsNullOrEmpty(statesString))
                {
                    var states = JsonSerializer.Deserialize <List <State> >(statesString,
                                                                            new JsonSerializerOptions {
                        PropertyNameCaseInsensitive = true
                    });
                    var customers = GetCustomers(states);
                    db.Customers.AddRange(customers);
                    int numAffected = await db.SaveChangesAsync();

                    _Logger.LogInformation($"Saved {numAffected} customers");
                }
                else
                {
                    _Logger.LogInformation("No states were found from lookup-api call");
                }
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(CustomersDbSeeder)}: " + exp.Message);
            }
        }
        public async Task InsertCustomersSampleData(CustomersDbContext db)
        {
            var httpClient   = new HttpClient();
            var statesString = await httpClient.GetStringAsync("http://localhost:5001/api/v1/lookup/states");

            var states    = JsonConvert.DeserializeObject <List <State> >(statesString);
            var customers = GetCustomers(states);

            db.Customers.AddRange(customers);

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

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