コード例 #1
0
 public static void SeedData(TechWallDbContext context)
 {
     SeedUsersAndRoles(context);
     SeedCategories(context);
     //SeedCommentsOrders(context);
     context.SaveChanges();
 }
コード例 #2
0
        private static void SeedCategories(TechWallDbContext context)
        {
            //,Categories = new List<Category>()
            #region SeedBrandsTable
            var brands = new List <Brand>
            {
                new Brand {
                    Name = "Apple"
                },
                new Brand {
                    Name = "Sony"
                },
                new Brand {
                    Name = "Huawei"
                },
                new Brand {
                    Name = "Nokia"
                },
                new Brand {
                    Name = "Dell"
                },
                new Brand {
                    Name = "Nikkon"
                },
                new Brand {
                    Name = "Canon"
                },
                new Brand {
                    Name = "Asus"
                },
                new Brand {
                    Name = "HP"
                },
                new Brand {
                    Name = "Xiaomi"
                },
                new Brand {
                    Name = "Alcatel"
                },
                new Brand {
                    Name = "Samsung"
                }
            };
            brands.ForEach(c => context.Brands.AddOrUpdate(p => p.Name, c));
            context.SaveChanges();
            #endregion

            #region SeedCategoriesTable
            var paraentCategories = new List <Category>();

            //SEED CATEGORIES
            //Parent Categories
            var Computers = new Category {
                Name = "Computers"
            };
            var Photography = new Category {
                Name = "Photography"
            };
            var MobileDevices = new Category {
                Name = "Mobile Devices"
            };

            paraentCategories.Add(Photography);
            paraentCategories.Add(Computers);
            paraentCategories.Add(MobileDevices);

            paraentCategories.ForEach(c => context.Categories.AddOrUpdate(p => p.Name, c));
            context.SaveChanges();

            var Laptops = new Category {
                Name = "Laptops", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Computers").ID
            };
            var Desktops = new Category {
                Name = "Desktops", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Computers").ID
            };

            var VideoCameras = new Category {
                Name = "Video Cameras", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Photography").ID
            };
            var PhotoCameras = new Category {
                Name = "Photo Cameras", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Photography").ID
            };

            var MobilePhones = new Category {
                Name = "Mobile Phones", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Mobile Devices").ID
            };
            var Tablets = new Category {
                Name = "Tablets", ParentCategoryID = context.Categories.Single(pc => pc.Name == "Mobile Devices").ID
            };

            var firstChildCategories = new List <Category>();

            firstChildCategories.Add(Laptops);
            firstChildCategories.Add(Desktops);
            firstChildCategories.Add(VideoCameras);
            firstChildCategories.Add(PhotoCameras);
            firstChildCategories.Add(MobilePhones);
            firstChildCategories.Add(Tablets);

            firstChildCategories.ForEach(c => context.Categories.AddOrUpdate(p => p.Name, c));
            context.SaveChanges();
            #endregion

            //#region AsscociateBrandsWithCategories

            //AddOrUpdateBrandCategory(context, "Sony", "Computers");
            //AddOrUpdateBrandCategory(context, "Sony", "Laptops");
            //AddOrUpdateBrandCategory(context, "Sony", "Desktops");
            //AddOrUpdateBrandCategory(context, "Sony", "Mobile Devices");
            //AddOrUpdateBrandCategory(context, "Sony", "Mobile Phones");
            //AddOrUpdateBrandCategory(context, "Sony", "Tablets");
            //AddOrUpdateBrandCategory(context, "Sony", "Photography");
            //AddOrUpdateBrandCategory(context, "Sony", "Photo Cameras");
            //AddOrUpdateBrandCategory(context, "Sony", "Video Cameras");

            //AddOrUpdateBrandCategory(context, "Xiaomi", "Computers");
            //AddOrUpdateBrandCategory(context, "Xiaomi", "Laptops");
            //AddOrUpdateBrandCategory(context, "Xiaomi", "Mobile Devices");

            //AddOrUpdateBrandCategory(context, "Apple", "Computers");
            //AddOrUpdateBrandCategory(context, "Apple", "Laptops");
            //AddOrUpdateBrandCategory(context, "Apple", "Desktops");
            //AddOrUpdateBrandCategory(context, "Apple", "Mobile Devices");
            //AddOrUpdateBrandCategory(context, "Apple", "Mobile Phones");
            //AddOrUpdateBrandCategory(context, "Apple", "Tablets");


            //AddOrUpdateBrandCategory(context, "Canon", "Photography");
            //AddOrUpdateBrandCategory(context, "Canon", "Photo Cameras");
            //AddOrUpdateBrandCategory(context, "Canon", "Video Cameras");

            //AddOrUpdateBrandCategory(context, "Asus", "Computers");
            //AddOrUpdateBrandCategory(context, "Asus", "Laptops");
            //AddOrUpdateBrandCategory(context, "Asus", "Desktops");
            //AddOrUpdateBrandCategory(context, "Asus", "Mobile Devices");
            //AddOrUpdateBrandCategory(context, "Asus", "Mobile Phones");
            //AddOrUpdateBrandCategory(context, "Asus", "Tablets");

            //context.SaveChanges();


            //#endregion



            #region SeedProducts
            var products = new List <Product>
            {
                new Product
                {
                    Name        = "IPhone 11 Pro Max 64GB",
                    Price       = 1339,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~//Content//img//Apple_Iphone11ProMax4gb.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~//Content//img//Apple_Iphone11ProMax4gb.jpg", Order = 2
                        }
                    }
                },

                new Product
                {
                    Name        = "IPhone 11 Pro Max 256GB",
                    Price       = 1539, Summary = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/Apple_Iphone11ProMax4gb.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/Apple_Iphone11ProMax4gb.jpg", Order = 2
                        }
                    }
                },

                new Product
                {
                    Name        = "IPhone 11 Pro Max 512GB",
                    Price       = 1789, Summary = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = "~//Content//img//Apple_Iphone11ProMax4gb.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/Apple_Iphone11ProMax4gb.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "IPhone 11 128GB",
                    Price       = 929,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/Apple_Iphone11ProMax4gb.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/Apple_Iphone11ProMax4gb.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Huawei P30 Lite 128GB",
                    Price       = 329,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Huawei").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product {
                    Name        = "Huawei P30 Pro 128GB",
                    Price       = 849,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Huawei").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Huawei Mate 20 Lite",
                    Price       = 249,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Huawei").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Mobile Phones").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Apple IPad 32GB",
                    Price       = 419,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Tablets").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Apple IPad Pro 64GB",
                    Price       = 1199,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Apple").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Tablets").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Huawei MediaPad M5",
                    Price       = 299,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Huawei").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Tablets").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Nikkon D5500",
                    Price       = 550,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Nikkon").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Photo Cameras").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Nikkon D750",
                    Price       = 625,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Nikkon").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Photo Cameras").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                },
                new Product
                {
                    Name        = "Samsung Galaxy Tab",
                    Price       = 299,
                    Summary     = "It is an iphone short",
                    Description = "It is an iphone long",
                    BrandID     = context.Brands.Single(b => b.Name == "Samsung").ID,
                    CategoryID  = context.Categories.Single(c => c.Name == "Tablets").ID,
                    Pictures    = new List <Picture>
                    {
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone1.jpg", Order = 1
                        },
                        new Picture()
                        {
                            URL = @"~/Content/img/iphone2.jpg", Order = 2
                        }
                    }
                }
            };

            products.ForEach(p => context.Products.AddOrUpdate(pp => pp.Name, p));
            context.SaveChanges();



            #endregion

            //#region Pictures
            //var pictures = new List<Picture>
            //{
            //    new Picture(){URL=@"~/Content/img/iphone1.jpg",ProductID= context.Products.Single(p=>p.Name=="Iphone").ID,Order=1},
            //    new Picture(){URL=@"~/Content/img/iphone2.jpg",ProductID= context.Products.Single(p=>p.Name=="Iphone 10").ID,Order=2}
            //};

            //new Picture() { URL = @"~/Content/img/iphone1.jpg", Order = 1 },
            //new Picture() { URL = @"~/Content/img/iphone2.jpg", Order = 2 }

            //pictures.ForEach(p => context.Pictures.AddOrUpdate(pp => pp.URL, p));
            //context.SaveChanges();

            //#endregion
        }