예제 #1
0
        public async Task Seed()
        {
            _ctx.Database.EnsureCreated();

            var user = await _userManager.FindByEmailAsync("*****@*****.**");

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

                // BY DEFAULT THERE ARE RULES ABOUT PASSWORD COMPLEXITY
                var result = await _userManager.CreateAsync(user, "P@ssw0rd!");

                if (result == IdentityResult.Success)
                {
                    throw new InvalidOperationException("failled to create");
                }
            }

            if (!_ctx.Products.Any())
            {
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/art.json");
                var json     = File.ReadAllText(filepath);

                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                _ctx.Products.AddRange(products);

                var order = new Order()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "12345",
                    User        = user,
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };
                _ctx.Orders.Add(order);
                _ctx.SaveChanges();
            }
        }
예제 #2
0
 public bool SaveAll()
 {
     return(_ctx.SaveChanges() > 0);
 }