Exemplo n.º 1
0
        public async Task SeedAsync()
        {
            Context.Database.EnsureCreated();

            UserShop user = await this.userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new UserShop
                {
                    FirstName = "Ahmad",
                    LastName  = "Haseeb",
                    Email     = "*****@*****.**",
                    UserName  = "******"
                };

                var result = await this.userManager.CreateAsync(user, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create new user In Seeder");
                }
            }

            if (!Context.Products.Any())
            {
                string filepath = Path.Combine(hosting.ContentRootPath, "Data/art.json");
                string json     = File.ReadAllText(filepath);
                var    products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                Context.AddRange(products);

                Order order = new Order()
                {
                    User        = user,
                    OrderDate   = DateTime.UtcNow,
                    OrderNumber = "12345",
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };
                Context.Add(order);
            }
            Context.SaveChanges();
        }