示例#1
0
        public async Task SeedAsync()
        {
            await _context.Database.EnsureCreatedAsync();

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

            if (user == null)
            {
                // Create new user
                user = new StoreUser()
                {
                    FirstName = "Seif",
                    LastName  = "Eddine",
                    Email     = "*****@*****.**",
                    UserName  = "******"
                };
                var result = await _userManager.CreateAsync(user, "Pa$$W0rd!");

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

            if (!_context.Products.Any())
            {
                // Need to seed dev data
                var FilePath = Path.Combine(_env.WebRootPath, "Data/art.json");
                var json     = File.ReadAllText(FilePath);
                var products = JsonSerializer.Deserialize <IEnumerable <Product> >(json);

                _context.Products.AddRange(products);

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

                await _context.SaveChangesAsync();
            }
        }
示例#2
0
        public async Task Seed()
        {
            await _context.Database.EnsureCreatedAsync();

            StoredUser user = await _userManager.FindByEmailAsync(_configuration["Defaults:User:Email"]);

            if (user == null)
            {
                user = new StoredUser
                {
                    FirtsName = _configuration["Defaults:User:Name"],
                    LastName  = _configuration["Defaults:User:LastName"],
                    Email     = _configuration["Defaults:User:Email"],
                    UserName  = _configuration["Defaults:User:Email"]
                };

                var result = await _userManager.CreateAsync(user, _configuration["Defaults:User:Password"]);

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

            if (_context.Products.Any())
            {
                return;
            }

            var products = await ListExampleProducts();

            await _context
            .Products
            .AddRangeAsync(products);

            await AddExampleOrder(user, products.FirstOrDefault());

            await _context.SaveChangesAsync();
        }
示例#3
0
 public async Task <bool> SaveAll()
 {
     return(await _context.SaveChangesAsync() > 0);
 }
 public async Task <bool> SaveAll() =>
 await _context.SaveChangesAsync() > 0;