Example #1
0
 public static void SeedData(StoreContext context)
 {
     try
     {
         if (!context.Categories.Any())
         {
             context.Categories.AddRange(StoreSampleData.GetCategories());
             context.SaveChanges();
         }
         if (!context.Products.Any())
         {
             context.Products.AddRange(
                 StoreSampleData.GetProducts(context.Categories.ToList()));
             context.SaveChanges();
         }
         if (!context.Customers.Any())
         {
             context.Customers.AddRange(
                 StoreSampleData.GetAllCustomerRecords(context));
             context.SaveChanges();
         }
         var customer = context.Customers.FirstOrDefault();
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex);
     }
 }
        internal static void SeedData(StoreContext context)
        {
            try
            {
                if (!context.Categories.Any())
                {
                    context.Categories.AddRange(StoreSampleData.GetCategories());
                    context.SaveChanges();
                }

                if (!context.Products.Any())
                {
                    context.Products.AddRange(
                        StoreSampleData.GetProducts(context.Categories.ToList()));
                    context.SaveChanges();
                }

                if (!context.Customers.Any())
                {
                    context.Customers.AddRange(
                        StoreSampleData.GetAllCustomerRecords(context));
                    context.SaveChanges();
                }

                var customer = context.Customers.FirstOrDefault();
                if (!context.Orders.Any())
                {
                    context.Orders.AddRange(StoreSampleData.GetOrders(customer, context));
                    context.SaveChanges();
                }

                if (!context.OrderDetails.Any())
                {
                    var order = context.Orders.First();
                    context.OrderDetails.AddRange(StoreSampleData.GetOrderDetails(order, context));
                    context.SaveChanges();
                }

                if (!context.ShoppingCartRecords.Any())
                {
                    context.ShoppingCartRecords.AddRange(
                        StoreSampleData.GetCart(customer, context));
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }