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

            // Seed the Main User
            User user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new User()
                {
                    Email    = "*****@*****.**",
                    UserName = "******"
                };

                var result = await _userManager.CreateAsync(user, "P@ssword01");

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

            if (!_ctx.Products.Any())
            {
                // Need to create sample data
                var filepath = Path.Combine(_hosting.ContentRootPath, "Models/cars.json");
                var json     = File.ReadAllText(filepath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                _ctx.Products.AddRange(products);

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

                _ctx.SaveChanges();
            }
        }
Exemplo n.º 2
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }