public void Sead()
        {
            _context.Database.EnsureCreated();

            if (!_context.Products.Any())
            {
                //need to create sample data
                var file     = Path.Combine(_hosting.ContentRootPath, "Data/art.json");
                var json     = File.ReadAllText(file);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                _context.Products.AddRange(products);

                var order = _context.Orders.Where(o => o.Id == 1).FirstOrDefault();
                if (order != null)
                {
                    order.Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 10,
                            UnitPrice = products.First().Price
                        }
                    };
                }

                _context.SaveChanges();
            }
        }
Exemple #2
0
 public bool SaveAll()
 {
     try
     {
         _logger.LogInformation("SaveAll is Called ...");
         return(_context.SaveChanges() > 0);
     }
     catch (Exception ex)
     {
         _logger.LogError($" Failed to Save: {ex.Message}");
         return(false);
     }
 }