Esempio n. 1
0
        public async Task Seed()
        {
            _context.Database.EnsureCreated();

            var userEmail = await _user.FindByEmailAsync("*****@*****.**");

            if (userEmail == null)
            {
                userEmail = new StoreUser()
                {
                    FirstName = "Usman",
                    LastName  = "Khan",
                    Email     = "*****@*****.**",
                    UserName  = "******"
                };
                var result = await _user.CreateAsync(userEmail, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Failed To Add Default User");
                }
            }



            if (!_context.Products.Any())
            {
                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 = new Order()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "12345",

                    Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product  = products.First(),
                            Quantity = 5,

                            UnitPrice = products.First().Price
                        }
                    }
                };
                _context.Orders.Add(order);
                _context.SaveChanges();
            }
        }
 public bool SaveAll()
 {
     return(_context.SaveChanges() > 0);
 }