public void Seed()
        {        //Before we try to issue any queries ensure that table are created
            _ctx.Database.EnsureCreated();
            if (!_ctx.Products.Any())
            {
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/products.json");
                var file     = File.ReadAllText(filepath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(file);
                _ctx.Products.AddRange(products);

                var order = new Order()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "12345",
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 2,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                _ctx.Orders.Add(order);
                _ctx.SaveChanges();
            }
        }
 public bool SaveAll()
 {
     try
     {
         _logger.LogInformation("Save all was called");
         return(_context.SaveChanges() > 0);
     }
     catch (Exception ex)
     {
         _logger.LogError($"An error was thrown in Save All {ex}");
         throw;
     }
 }
Exemplo n.º 3
0
        public async Task Seed()
        {        //Before we try to issue any queries ensure that table are created
            _ctx.Database.EnsureCreated();

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

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

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

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Failed to create default user");
                }
            }

            if (!_ctx.Products.Any())
            {
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/products.json");
                var file     = File.ReadAllText(filepath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(file);
                _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  = 2,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                _ctx.Orders.Add(order);
                _ctx.SaveChanges();
            }
        }
Exemplo n.º 4
0
        public async Task Seed()
        {
            _ctx.Database.EnsureCreated();

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

            if (user == null)
            {
                user = new StoreUser()
                {
                    FirstName = "Test",
                    LastName  = "User",
                    UserName  = "******",
                    Email     = "*****@*****.**"
                };
                var result = await _userManager.CreateAsync(user, "P@ssw0rd!");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Failed to create default user.");
                }
            }
            if (!_ctx.Products.Any())
            {
                // Need to create sample data
                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,
                    User        = user,
                    OrderNumber = "12345",
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                _ctx.Orders.Add(order);
                _ctx.SaveChanges();
            }
        }
Exemplo n.º 5
0
 public bool SaveAll()
 {
     return(context.SaveChanges() > 0);
 }
Exemplo n.º 6
0
 public bool SaveAll()
 {
     return(ctx.SaveChanges() > 0);
 }