Exemple #1
0
        public static async Task SeedOrders(ShopsRUsDbContext context)
        {
            if (await context.Orders.AnyAsync())
            {
                return;
            }

            string orderData = default(string);

            //orderData = await System.IO.File.ReadAllTextAsync("../ShopsRUs.Infrastructure/OrderSeedData.json");
            orderData = await System.IO.File.ReadAllTextAsync(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "OrderSeedData.json"));

            List <Int64> listIds = new List <Int64>();

            var orders = JsonSerializer.Deserialize <List <Order> >(orderData);

            var customers = await context.Customers.ToListAsync();

            customers.ForEach(x =>
            {
                listIds.Add(x.Id);
            });

            foreach (var order in orders)
            {
                //Int64[] newList = Array.ConvertAll(list.Split(','), s => Int64.Parse(s));
                Random random = new Random();
                int    rnd    = random.Next(0, listIds.Count);
                order.CustomerId = listIds[rnd];

                context.Orders.Add(order);
            }

            await context.SaveChangesAsync();
        }
Exemple #2
0
        public static async Task SeedCustomers(ShopsRUsDbContext context)
        {
            if (await context.Customers.AnyAsync())
            {
                return;
            }

            string customerData     = default(string);
            string currentDirectory = Directory.GetCurrentDirectory();
            int    index            = currentDirectory.LastIndexOf('\\');

            //customerData = await System.IO.File.ReadAllTextAsync("../ShopsRUs.Infrastructure/CustomerSeedData.json");
            customerData = await System.IO.File.ReadAllTextAsync(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "CustomerSeedData.json"));


            var customers = JsonSerializer.Deserialize <List <Customer> >(customerData);

            foreach (var customer in customers)
            {
                customer.CustomerName = customer.CustomerName.ToLower();

                context.Customers.Add(customer);
            }

            context.SaveChanges();
        }
Exemple #3
0
        public static async Task SeedDiscounts(ShopsRUsDbContext context)
        {
            if (await context.Discounts.AnyAsync())
            {
                return;
            }

            string discountData = default(string);

            //discountData = await System.IO.File.ReadAllTextAsync("../ShopsRUs.Infrastructure/DiscountSeedData.json");
            discountData = await System.IO.File.ReadAllTextAsync(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DiscountSeedData.json"));


            var discounts = JsonSerializer.Deserialize <List <Discount> >(discountData);

            foreach (var discount in discounts)
            {
                discount.DiscountType = discount.DiscountType.ToLower();

                context.Discounts.Add(discount);
            }

            await context.SaveChangesAsync();
        }