コード例 #1
0
        private static void ensureBaseUsers(
            WebStoreContext context)
        {
            var userStore   = new UserStore <User>(context);
            var userManager = new UserManager <User>(
                userStore,
                new OptionsManager <IdentityOptions>(
                    new OptionsFactory <IdentityOptions>(
                        new IConfigureOptions <IdentityOptions>[] { },
                        new IPostConfigureOptions <IdentityOptions>[] { })),
                new PasswordHasher <User>(),
                new IUserValidator <User>[] { },
                new IPasswordValidator <User>[] { },
                new UpperInvariantLookupNormalizer(),
                new IdentityErrorDescriber(),
                null,
                null);

            if (userStore
                .FindByEmailAsync("*****@*****.**", CancellationToken.None)
                .Result == null)
            {
                var user = new User()
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                };
                var result = userManager.CreateAsync(user, "Pa$$w0rd").Result;
                if (result == IdentityResult.Success)
                {
                    _ = userManager
                        .AddToRoleAsync(user, "Administrators")
                        .Result;
                }
            }
            if (userStore
                .FindByEmailAsync("*****@*****.**", CancellationToken.None)
                .Result == null)
            {
                var user = new User()
                {
                    UserName = "******",
                    Email    = "*****@*****.**"
                };
                var result = userManager.CreateAsync(user, "Pa$$w0rd").Result;
                if (result == IdentityResult.Success)
                {
                    _ = userManager
                        .AddToRoleAsync(user, "Users")
                        .Result;
                }
            }
        }
コード例 #2
0
        public static void Initialize(WebStoreContext webStoreContext)
        {
            webStoreContext.Database.EnsureCreated();

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

            InitCategories(webStoreContext);
            InitBrands(webStoreContext);
            InitProducts(webStoreContext);
        }
コード例 #3
0
        public static async Task InitializeBaseAsync(WebStoreContext context)
        {
            //context.Database.EnsureCreated();
            //await context.Database.EnsureCreatedAsync();
            //await context.Database.MigrateAsync(); // Автоматическое создание и миграция базы до последней версии

            //IEnumerable<PropertyInfo> dbSetProperties =
            //    context.GetType()
            //        .GetProperties()
            //        .Where(p => p.PropertyType.Name.StartsWith("DbSet"));

            List <string> dbSetProperties = new List <string>()
            {
                "Brands", "Categories", "Products"
            };

            foreach (string tableName in dbSetProperties)
            {
                switch (tableName)
                {
                case "Products":
                    if (!context.Products.Any())
                    {
                        await FillTable <Product>(context, WebStore.Data.TestData.Products, tableName);
                    }
                    break;

                case "Categories":
                    if (!context.Categories.Any())
                    {
                        await FillTable <Category>(context, WebStore.Data.TestData.Categories, tableName);
                    }
                    break;

                case "Brands":
                    if (!context.Brands.Any())
                    {
                        await FillTable <Brand>(context, WebStore.Data.TestData.Brands, tableName);
                    }
                    break;
                }
            }
        }
コード例 #4
0
        // privates

        private static void ensureBaseRoles(
            WebStoreContext context)
        {
            var roleStore   = new RoleStore <IdentityRole>(context);
            var roleManager = new RoleManager <IdentityRole>(
                roleStore,
                new IRoleValidator <IdentityRole>[] { },
                new UpperInvariantLookupNormalizer(),
                new IdentityErrorDescriber(),
                null);

            if (!roleManager.RoleExistsAsync("Users").Result)
            {
                _ = roleManager.CreateAsync(new IdentityRole("Users")).Result;
            }
            if (!roleManager.RoleExistsAsync("Administrators").Result)
            {
                _ = roleManager.CreateAsync(new IdentityRole("Administrators")).Result;
            }
        }
コード例 #5
0
        private static async Task FillTable <T>(WebStoreContext context, IEnumerable <T> entList, string tableName)
        {
            bool commitNeed = true;

            using (var trans = await context.Database.BeginTransactionAsync())
            {
                switch (tableName)
                {
                case "Products":
                    context.Products.AddRange((IEnumerable <Product>)entList);
                    break;

                case "Categories":
                    context.Categories.AddRange((IEnumerable <Category>)entList);
                    break;

                case "Brands":
                    context.Brands.AddRange((IEnumerable <Brand>)entList);
                    break;

                default:
                    commitNeed = false;
                    break;
                }

                if (commitNeed)
                {
                    await context.Database.ExecuteSqlRawAsync(String.Format("SET IDENTITY_INSERT [dbo].[{0}] ON", tableName));

                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync(String.Format("SET IDENTITY_INSERT [dbo].[{0}] OFF", tableName));

                    trans.Commit();
                }
            }
        }
コード例 #6
0
        public static void Initialize(WebStoreContext context)
        {
            context.Database.EnsureCreated();

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

            // заполнение БД данными
            var _sections = new List <Section>
            {
                new Section()
                {
                    Id       = 1,
                    Name     = "SportsWear",
                    Order    = 0,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 2,
                    Name     = "Nike",
                    Order    = 0,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 3,
                    Name     = "Under Armour",
                    Order    = 1,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 4,
                    Name     = "Adidas",
                    Order    = 2,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 5,
                    Name     = "Puma",
                    Order    = 3,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 7,
                    Name     = "Mens",
                    Order    = 1,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 8,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 9,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 10,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 11,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 12,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 13,
                    Name     = "Armani",
                    Order    = 5,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 14,
                    Name     = "Prada",
                    Order    = 6,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 15,
                    Name     = "Dolce and Gabbana",
                    Order    = 7,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 16,
                    Name     = "Chanel",
                    Order    = 8,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 17,
                    Name     = "Gucci",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 18,
                    Name     = "Womens",
                    Order    = 2,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 19,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 20,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 21,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 22,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 23,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 24,
                    Name     = "Kids",
                    Order    = 3,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 25,
                    Name     = "Fashion",
                    Order    = 4,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 26,
                    Name     = "Households",
                    Order    = 5,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 27,
                    Name     = "Interiors",
                    Order    = 6,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 28,
                    Name     = "Clothing",
                    Order    = 7,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 29,
                    Name     = "Bags",
                    Order    = 8,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 30,
                    Name     = "Shoes",
                    Order    = 9,
                    ParentId = null
                }
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections ON");

                foreach (var section in _sections)
                {
                    context.Sections.Add(section);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections OFF");

                trans.Commit();
            }

            var _brands = new List <Brand>()
            {
                new Brand()
                {
                    Id    = 1,
                    Name  = "Acne",
                    Order = 0
                },
                new Brand()
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1
                },
                new Brand()
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2
                },
                new Brand()
                {
                    Id    = 4,
                    Name  = "Ronhill",
                    Order = 3
                },
                new Brand()
                {
                    Id    = 5,
                    Name  = "Oddmolly",
                    Order = 4
                },
                new Brand()
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 5
                },
                new Brand()
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 6
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands ON");

                foreach (var brand in _brands)
                {
                    context.Brands.Add(brand);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands OFF");

                trans.Commit();
            }

            var _products = new List <Product>()
            {
                new Product()
                {
                    Id        = 1,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product1.jpg",
                    Order     = 0,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 2,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product2.jpg",
                    Order     = 1,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 3,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product3.jpg",
                    Order     = 2,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 4,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product4.jpg",
                    Order     = 3,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 5,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product5.jpg",
                    Order     = 4,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 6,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product6.jpg",
                    Order     = 5,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 7,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product7.jpg",
                    Order     = 6,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 8,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product8.jpg",
                    Order     = 7,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 9,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product9.jpg",
                    Order     = 8,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 10,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product10.jpg",
                    Order     = 9,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 11,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product11.jpg",
                    Order     = 10,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 12,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product12.jpg",
                    Order     = 11,
                    SectionId = 25,
                    BrandId   = 3
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products ON");

                foreach (var product in _products)
                {
                    context.Products.Add(product);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products OFF");

                trans.Commit();
            }
        }
コード例 #7
0
        public static void Initializre(WebStoreContext context)
        {
            context.Database.Migrate();

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

            //Заполнение БД
            var _brands = new List <Brand>
            {
                new Brand
                {
                    Id    = 1,
                    Name  = "ACNE",
                    Order = 0
                },
                new Brand
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1,
                },
                new Brand
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2,
                },
                new Brand
                {
                    Id    = 5,
                    Name  = "Ronhill",
                    Order = 3,
                },
                new Brand
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 4,
                },
                new Brand
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 5,
                }
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands ON");

                foreach (var brand in _brands)
                {
                    context.Brands.Add(brand);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands OFF");

                trans.Commit();
            }

            var _sections = new List <Section>
            {
                new Section
                {
                    Id       = 1,
                    Name     = "SPORTSWEAR",
                    Order    = 0,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 2,
                    Name     = "NIKE",
                    Order    = 0,
                    ParentId = 1,
                },
                new Section
                {
                    Id       = 3,
                    Name     = "UNDER ARMOUR",
                    Order    = 1,
                    ParentId = 1,
                },
                new Section
                {
                    Id       = 4,
                    Name     = "ADIDAS",
                    Order    = 2,
                    ParentId = 1,
                },
                new Section
                {
                    Id       = 5,
                    Name     = "PUMA",
                    Order    = 3,
                    ParentId = 1,
                },
                new Section
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1,
                },
                new Section
                {
                    Id       = 7,
                    Name     = "MENS",
                    Order    = 1,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 8,
                    Name     = "FENDI",
                    Order    = 0,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 9,
                    Name     = "GUESS",
                    Order    = 1,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 10,
                    Name     = "VALENTINO",
                    Order    = 2,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 11,
                    Name     = "DIOR",
                    Order    = 3,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 12,
                    Name     = "VERSACE",
                    Order    = 4,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 13,
                    Name     = "ARMANI",
                    Order    = 5,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 14,
                    Name     = "PRADA",
                    Order    = 6,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 15,
                    Name     = "DOLCE AND GABBANA",
                    Order    = 7,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 16,
                    Name     = "CHANEL",
                    Order    = 8,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 17,
                    Name     = "GUCCI",
                    Order    = 9,
                    ParentId = 7,
                },
                new Section
                {
                    Id       = 18,
                    Name     = "WOMENS",
                    Order    = 2,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 19,
                    Name     = "FENDI",
                    Order    = 0,
                    ParentId = 18,
                },
                new Section
                {
                    Id       = 20,
                    Name     = "GUESS",
                    Order    = 1,
                    ParentId = 18,
                },
                new Section
                {
                    Id       = 21,
                    Name     = "VALENTINO",
                    Order    = 2,
                    ParentId = 18,
                },
                new Section
                {
                    Id       = 22,
                    Name     = "DIOR",
                    Order    = 3,
                    ParentId = 18,
                },
                new Section
                {
                    Id       = 23,
                    Name     = "VERSACE",
                    Order    = 4,
                    ParentId = 18,
                },
                new Section
                {
                    Id       = 24,
                    Name     = "KIDS",
                    Order    = 3,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 25,
                    Name     = "FASHION",
                    Order    = 4,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 26,
                    Name     = "HOUSEHOLDS",
                    Order    = 5,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 27,
                    Name     = "INTERIORS",
                    Order    = 6,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 28,
                    Name     = "CLOTHING",
                    Order    = 7,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 29,
                    Name     = "BAGS",
                    Order    = 8,
                    ParentId = null,
                },
                new Section
                {
                    Id       = 30,
                    Name     = "SHOES",
                    Order    = 9,
                    ParentId = null,
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections ON");

                foreach (var section in _sections)
                {
                    context.Sections.Add(section);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections OFF");

                trans.Commit();
            }

            var _products = new List <Product>
            {
                new Product
                {
                    Id        = 1,
                    Name      = "Easy Polo Black Edition",
                    Order     = 0,
                    Price     = 15,
                    BrandId   = 1,
                    SectionId = 24,
                    ImageUrl  = "product12.jpg"
                },
                new Product
                {
                    Id        = 2,
                    Name      = "Easy Polo Black Edition",
                    Order     = 1,
                    Price     = 20,
                    BrandId   = 1,
                    SectionId = 24,
                    ImageUrl  = "product11.jpg"
                },
                new Product
                {
                    Id        = 3,
                    Name      = "Easy Polo Black Edition",
                    Order     = 2,
                    Price     = 25,
                    BrandId   = 2,
                    SectionId = 2,
                    ImageUrl  = "product10.jpg"
                },
                new Product
                {
                    Id        = 4,
                    Name      = "Easy Polo Black Edition",
                    Order     = 3,
                    Price     = 30,
                    BrandId   = 2,
                    SectionId = 2,
                    ImageUrl  = "product9.jpg"
                },
                new Product
                {
                    Id        = 5,
                    Name      = "Easy Polo Black Edition",
                    Order     = 4,
                    Price     = 35,
                    BrandId   = 2,
                    SectionId = 4,
                    ImageUrl  = "product8.jpg"
                },
                new Product
                {
                    Id         = 6,
                    Name       = "Easy Polo Black Edition",
                    Order      = 5,
                    Price      = 40,
                    BrandId    = 2,
                    SectionId  = 4,
                    ImageUrl   = "product1.jpg",
                    StatusHome = true
                },
                new Product
                {
                    Id         = 7,
                    Name       = "Easy Polo Black Edition",
                    Order      = 6,
                    Price      = 40,
                    BrandId    = 2,
                    SectionId  = 4,
                    ImageUrl   = "product2.jpg",
                    StatusHome = true
                },
                new Product
                {
                    Id         = 8,
                    Name       = "Easy Polo Black Edition",
                    Order      = 7,
                    Price      = 40,
                    BrandId    = 2,
                    SectionId  = 4,
                    ImageUrl   = "product3.jpg",
                    StatusHome = true
                },
                new Product
                {
                    Id        = 9,
                    Name      = "Easy Polo Black Edition",
                    Order     = 8,
                    Price     = 40,
                    BrandId   = 2,
                    SectionId = 4,
                    ImageUrl  = "product4.jpg",
                    StatusNew = true
                },
                new Product
                {
                    Id         = 10,
                    Name       = "Easy Polo Black Edition",
                    Order      = 9,
                    Price      = 40,
                    BrandId    = 2,
                    SectionId  = 4,
                    ImageUrl   = "product5.jpg",
                    StatusSale = true
                },
                new Product
                {
                    Id         = 11,
                    Name       = "Easy Polo Black Edition",
                    Order      = 10,
                    Price      = 40,
                    BrandId    = 2,
                    SectionId  = 4,
                    ImageUrl   = "product6.jpg",
                    StatusHome = true
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products ON");

                foreach (var product in _products)
                {
                    context.Products.Add(product);
                }

                context.SaveChanges();

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products OFF");

                trans.Commit();
            }
        }
コード例 #8
0
        public static void Initialize(WebStoreContext context)
        {
            //Если базы данных не существует, то метод создаст ее, если база данных есть то не будет создавать
            context.Database.EnsureCreated();
            if (context.Products.Any())
            {
                return;
            }

            //Если база данных пустая, то заполним ее

            var _sections = new List <Section>
            {
                new Section()
                {
                    Id       = 1,
                    Name     = "SportsWear",
                    Order    = 0,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 2,
                    Name     = "Nike",
                    Order    = 0,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 3,
                    Name     = "Under Armour",
                    Order    = 1,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 4,
                    Name     = "Adidas",
                    Order    = 2,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 5,
                    Name     = "Puma",
                    Order    = 3,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 7,
                    Name     = "Mens",
                    Order    = 1,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 8,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 9,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 10,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 11,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 12,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 13,
                    Name     = "Armani",
                    Order    = 5,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 14,
                    Name     = "Prada",
                    Order    = 6,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 15,
                    Name     = "Dolce and Gabbana",
                    Order    = 7,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 16,
                    Name     = "Chanel",
                    Order    = 8,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 17,
                    Name     = "Gucci",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 18,
                    Name     = "Womens",
                    Order    = 2,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 19,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 20,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 21,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 22,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 23,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 24,
                    Name     = "Kids",
                    Order    = 3,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 25,
                    Name     = "Fashion",
                    Order    = 4,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 26,
                    Name     = "Households",
                    Order    = 5,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 27,
                    Name     = "Interiors",
                    Order    = 6,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 28,
                    Name     = "Clothing",
                    Order    = 7,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 29,
                    Name     = "Bags",
                    Order    = 8,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 30,
                    Name     = "Shoes",
                    Order    = 9,
                    ParentId = null
                }
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections ON");
                foreach (var section in _sections)
                {
                    context.Sections.Add(section);
                }
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Sections OFF");
                trans.Commit();
            }
            var _brands = new List <Brand>()
            {
                new Brand()
                {
                    Id    = 1,
                    Name  = "Acne",
                    Order = 0
                },
                new Brand()
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1
                },
                new Brand()
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2
                },
                new Brand()
                {
                    Id    = 4,
                    Name  = "Ronhill",
                    Order = 3
                },
                new Brand()
                {
                    Id    = 5,
                    Name  = "Oddmolly",
                    Order = 4
                },
                new Brand()
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 5
                },
                new Brand()
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 6
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands ON");
                foreach (var brand in _brands)
                {
                    context.Brands.Add(brand);
                }
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Brands OFF");
                trans.Commit();
            }
            var _products = new List <Product>()
            {
                new Product()
                {
                    Id        = 1,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product1.jpg",
                    Order     = 0,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 2,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product2.jpg",
                    Order     = 1,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 3,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product3.jpg",
                    Order     = 2,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 4,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product4.jpg",
                    Order     = 3,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 5,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product5.jpg",
                    Order     = 4,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 6,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product6.jpg",
                    Order     = 5,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 7,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product7.jpg",
                    Order     = 6,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 8,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product8.jpg",
                    Order     = 7,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 9,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product9.jpg",
                    Order     = 8,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 10,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product10.jpg",
                    Order     = 9,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 11,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product11.jpg",
                    Order     = 10,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 12,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product12.jpg",
                    Order     = 11,
                    SectionId = 25,
                    BrandId   = 3
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products ON");
                foreach (var product in _products)
                {
                    context.Products.Add(product);
                }
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Products OFF");
                trans.Commit();
            }
            var _applicationUsers = new List <ApplicationUser>()
            {
                new ApplicationUser
                {
                    Id         = 1,
                    FirstName  = "Иван",
                    SurName    = "Иванов",
                    Patronomic = "Иванов",
                    Age        = 22,
                    Male       = true
                },
                new ApplicationUser
                {
                    Id         = 2,
                    FirstName  = "Петр",
                    SurName    = "Петров",
                    Patronomic = "Петрович",
                    Age        = 27,
                    Male       = true
                },
                new ApplicationUser
                {
                    Id         = 3,
                    FirstName  = "Сидор",
                    SurName    = "Сидоров",
                    Patronomic = "Сидорович",
                    Age        = 12,
                    Male       = true
                }
            };

            using (var trans = context.Database.BeginTransaction())
            {
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Users ON");
                foreach (var user in _applicationUsers)
                {
                    context.ApplicationUsers.Add(user);
                }
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].Users OFF");
                trans.Commit();
            }
        }
コード例 #9
0
        /// <summary>
        /// Наполняет таблицы БД начальными данными
        /// </summary>
        /// <param name="webStoreContext"></param>
        public static void Initialize(WebStoreContext webStoreContext)
        {
            //проверяет, создана ли БД (если нет, то создает ее? непонятно зачем, если нужно апдейтить через миграции, а не так)
            webStoreContext.Database.EnsureCreated();

            //Проверяет - если есть хоть один элемент в таблице, то ничего не делаем
            if (webStoreContext.Products.Any())
            {
                return;
            }

            //просто список с данными, которыми хотим напонить таблицу в базе
            List <Section> sections = new List <Section>()
            {
                new Section()
                {
                    Id       = 1,
                    Name     = "Sportswear",
                    Order    = 0,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 2,
                    Name     = "Nike",
                    Order    = 0,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 3,
                    Name     = "Under Armour",
                    Order    = 1,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 4,
                    Name     = "Adidas",
                    Order    = 2,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 5,
                    Name     = "Puma",
                    Order    = 3,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1
                },
                new Section()
                {
                    Id       = 7,
                    Name     = "Mens",
                    Order    = 1,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 8,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 9,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 10,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 11,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 12,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 13,
                    Name     = "Armani",
                    Order    = 5,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 14,
                    Name     = "Prada",
                    Order    = 6,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 15,
                    Name     = "Dolce and Gabbana",
                    Order    = 7,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 16,
                    Name     = "Chanel",
                    Order    = 8,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 17,
                    Name     = "Gucci",
                    Order    = 1,
                    ParentId = 7
                },
                new Section()
                {
                    Id       = 18,
                    Name     = "Womens",
                    Order    = 2,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 19,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 20,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 21,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 22,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 23,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 18
                },
                new Section()
                {
                    Id       = 24,
                    Name     = "Kids",
                    Order    = 3,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 25,
                    Name     = "Fashion",
                    Order    = 4,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 26,
                    Name     = "Households",
                    Order    = 5,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 27,
                    Name     = "Interiors",
                    Order    = 6,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 28,
                    Name     = "Clothing",
                    Order    = 7,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 29,
                    Name     = "Bags",
                    Order    = 8,
                    ParentId = null
                },
                new Section()
                {
                    Id       = 30,
                    Name     = "Shoes",
                    Order    = 9,
                    ParentId = null
                }
            };

            //перенос данных в базу
            using (IDbContextTransaction transaction = webStoreContext.Database.BeginTransaction())
            {
                //переносим данные из List в Context-таблицу
                foreach (Section section in sections)
                {
                    webStoreContext.Sections.Add(section);
                }

                //следующие команды нужны для того, чтобы можно было сохранить в полях Id таблицы БД свои значения
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sections] ON"); //отключаем проверку внешних ключей
                webStoreContext.SaveChanges();                                                         //сохраняем изменения, сделанные в Context, в БД
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Sections] OFF");

                //сохраняем данные о проведенных изменениях?
                transaction.Commit();
            }

            List <Brand> brands = new List <Brand>()
            {
                new Brand()
                {
                    Id    = 1,
                    Name  = "Acne",
                    Order = 0
                },
                new Brand()
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1
                },
                new Brand()
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2
                },
                new Brand()
                {
                    Id    = 4,
                    Name  = "Ronhill",
                    Order = 3
                },
                new Brand()
                {
                    Id    = 5,
                    Name  = "Oddmolly",
                    Order = 4
                },
                new Brand()
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 5
                },
                new Brand()
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 6
                },
            };

            using (var transaction = webStoreContext.Database.BeginTransaction())
            {
                foreach (Brand brand in brands)
                {
                    webStoreContext.Brands.Add(brand);
                }

                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] ON");
                webStoreContext.SaveChanges();
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] OFF");
                transaction.Commit();
            }

            List <Product> products = new List <Product>()
            {
                new Product()
                {
                    Id        = 1,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product1.jpg",
                    Order     = 0,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 2,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product2.jpg",
                    Order     = 1,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 3,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product3.jpg",
                    Order     = 2,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 4,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product4.jpg",
                    Order     = 3,
                    SectionId = 2,
                    BrandId   = 1
                },
                new Product()
                {
                    Id        = 5,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product5.jpg",
                    Order     = 4,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 6,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product6.jpg",
                    Order     = 5,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 7,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product7.jpg",
                    Order     = 6,
                    SectionId = 2,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 8,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product8.jpg",
                    Order     = 7,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 9,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product9.jpg",
                    Order     = 8,
                    SectionId = 25,
                    BrandId   = 2
                },
                new Product()
                {
                    Id        = 10,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product10.jpg",
                    Order     = 9,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 11,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product11.jpg",
                    Order     = 10,
                    SectionId = 25,
                    BrandId   = 3
                },
                new Product()
                {
                    Id        = 12,
                    Name      = "Easy Polo Black Edition",
                    Price     = 1025,
                    ImageUrl  = "product12.jpg",
                    Order     = 11,
                    SectionId = 25,
                    BrandId   = 3
                },
            };

            using (var transaction = webStoreContext.Database.BeginTransaction())
            {
                foreach (Product product in products)
                {
                    webStoreContext.Products.Add(product);
                }
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] ON");
                webStoreContext.SaveChanges();
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] OFF");
                transaction.Commit();
            }
        }
コード例 #10
0
        public static void Initialize(WebStoreContext context)
        {
            context.Database.EnsureCreated();
            // Look for any products.
            if (context.Products.Any())
            {
                return;   // DB had already been seeded
            }

            var categories = new List <Category>()
            {
                new Category()
                {
                    Id       = 1,
                    Name     = "Sportswear",
                    Order    = 0,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 2,
                    Name     = "Nike",
                    Order    = 0,
                    ParentId = 1
                },
                new Category()
                {
                    Id       = 3,
                    Name     = "Under Armour",
                    Order    = 1,
                    ParentId = 1
                },
                new Category()
                {
                    Id       = 4,
                    Name     = "Adidas",
                    Order    = 2,
                    ParentId = 1
                },
                new Category()
                {
                    Id       = 5,
                    Name     = "Puma",
                    Order    = 3,
                    ParentId = 1
                },
                new Category()
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1
                },
                new Category()
                {
                    Id       = 7,
                    Name     = "Mens",
                    Order    = 1,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 8,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 9,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 10,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 11,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 12,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 13,
                    Name     = "Armani",
                    Order    = 5,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 14,
                    Name     = "Prada",
                    Order    = 6,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 15,
                    Name     = "Dolce and Gabbana",
                    Order    = 7,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 16,
                    Name     = "Chanel",
                    Order    = 8,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 17,
                    Name     = "Gucci",
                    Order    = 1,
                    ParentId = 7
                },
                new Category()
                {
                    Id       = 18,
                    Name     = "Womens",
                    Order    = 2,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 19,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 18
                },
                new Category()
                {
                    Id       = 20,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 18
                },
                new Category()
                {
                    Id       = 21,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 18
                },
                new Category()
                {
                    Id       = 22,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 18
                },
                new Category()
                {
                    Id       = 23,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 18
                },
                new Category()
                {
                    Id       = 24,
                    Name     = "Kids",
                    Order    = 3,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 25,
                    Name     = "Fashion",
                    Order    = 4,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 26,
                    Name     = "Households",
                    Order    = 5,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 27,
                    Name     = "Interiors",
                    Order    = 6,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 28,
                    Name     = "Clothing",
                    Order    = 7,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 29,
                    Name     = "Bags",
                    Order    = 8,
                    ParentId = null
                },
                new Category()
                {
                    Id       = 30,
                    Name     = "Shoes",
                    Order    = 9,
                    ParentId = null
                }
            };

            using (var trans = context.Database.BeginTransaction())
            {
                foreach (var section in categories)
                {
                    context.Categories.Add(section);
                }

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Categories] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Categories] OFF");
                trans.Commit();
            }

            var brands = new List <Brand>()
            {
                new Brand()
                {
                    Id    = 1,
                    Name  = "Acne",
                    Order = 0
                },
                new Brand()
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1
                },
                new Brand()
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2
                },
                new Brand()
                {
                    Id    = 4,
                    Name  = "Ronhill",
                    Order = 3
                },
                new Brand()
                {
                    Id    = 5,
                    Name  = "Oddmolly",
                    Order = 4
                },
                new Brand()
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 5
                },
                new Brand()
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 6
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                foreach (var brand in brands)
                {
                    context.Brands.Add(brand);
                }

                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] OFF");
                trans.Commit();
            }

            var products = new List <Product>()
            {
                new Product()
                {
                    Id          = 1,
                    Name        = "Easy Polo 22",
                    Price       = 1025,
                    ImageUrl    = "product1.jpg",
                    Order       = 0,
                    CategoryId  = 2,
                    BrandId     = 1,
                    Description = "Description"
                },
                new Product()
                {
                    Id         = 2,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product2.jpg",
                    Order      = 1,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product()
                {
                    Id         = 3,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product3.jpg",
                    Order      = 2,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product()
                {
                    Id         = 4,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product4.jpg",
                    Order      = 3,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product()
                {
                    Id         = 5,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product5.jpg",
                    Order      = 4,
                    CategoryId = 2,
                    BrandId    = 2
                },
                new Product()
                {
                    Id         = 6,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product6.jpg",
                    Order      = 5,
                    CategoryId = 2,
                    BrandId    = 2
                },
                new Product()
                {
                    Id         = 7,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product7.jpg",
                    Order      = 6,
                    CategoryId = 2,
                    BrandId    = 2
                },
                new Product()
                {
                    Id         = 8,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product8.jpg",
                    Order      = 7,
                    CategoryId = 25,
                    BrandId    = 2
                },
                new Product()
                {
                    Id         = 9,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product9.jpg",
                    Order      = 8,
                    CategoryId = 25,
                    BrandId    = 2
                },
                new Product()
                {
                    Id         = 10,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product10.jpg",
                    Order      = 9,
                    CategoryId = 25,
                    BrandId    = 3
                },
                new Product()
                {
                    Id         = 11,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product11.jpg",
                    Order      = 10,
                    CategoryId = 25,
                    BrandId    = 3
                },
                new Product()
                {
                    Id         = 12,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product12.jpg",
                    Order      = 11,
                    CategoryId = 25,
                    BrandId    = 3
                },
            };

            using (var trans = context.Database.BeginTransaction())
            {
                foreach (var product in products)
                {
                    context.Products.Add(product);
                }
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] OFF");
                trans.Commit();
            }
        }
コード例 #11
0
 public static void InitializeUsers(
     WebStoreContext context)
 {
     ensureBaseRoles(context);
     ensureBaseUsers(context);
 }
コード例 #12
0
        public static void Initialize(
            WebStoreContext context)
        {
            context.Database.EnsureCreated();

            // categories
            if (!context.Categories.Any())
            {
                var categories = new List <Category>
                {
                    new Category {
                        Id = 1, Name = "Sportswear 2", Order = 0, ParentId = null
                    },
                    new Category {
                        Id = 2, Name = "Nike", Order = 0, ParentId = 1
                    },
                    new Category {
                        Id = 3, Name = "Under Armour", Order = 1, ParentId = 1
                    },
                    new Category {
                        Id = 4, Name = "Adidas", Order = 2, ParentId = 1
                    },
                    new Category {
                        Id = 5, Name = "Puma", Order = 3, ParentId = 1
                    },
                    new Category {
                        Id = 6, Name = "ASICS", Order = 4, ParentId = 1
                    },
                    new Category {
                        Id = 7, Name = "Mens", Order = 1, ParentId = null
                    },
                    new Category {
                        Id = 8, Name = "Fendi", Order = 0, ParentId = 7
                    },
                    new Category {
                        Id = 9, Name = "Guess", Order = 1, ParentId = 7
                    },
                    new Category {
                        Id = 10, Name = "Valentino", Order = 2, ParentId = 7
                    },
                    new Category {
                        Id = 11, Name = "Dior", Order = 3, ParentId = 7
                    },
                    new Category {
                        Id = 12, Name = "Versace", Order = 4, ParentId = 7
                    },
                    new Category {
                        Id = 13, Name = "Armani", Order = 5, ParentId = 7
                    },
                    new Category {
                        Id = 14, Name = "Prada", Order = 6, ParentId = 7
                    },
                    new Category {
                        Id = 15, Name = "Dolce and Gabbana", Order = 7, ParentId = 7
                    },
                    new Category {
                        Id = 16, Name = "Chanel", Order = 8, ParentId = 7
                    },
                    new Category {
                        Id = 17, Name = "Gucci", Order = 1, ParentId = 7
                    },
                    new Category {
                        Id = 18, Name = "Womens", Order = 2, ParentId = null
                    },
                    new Category {
                        Id = 19, Name = "Fendi", Order = 0, ParentId = 18
                    },
                    new Category {
                        Id = 20, Name = "Guess", Order = 1, ParentId = 18
                    },
                    new Category {
                        Id = 21, Name = "Valentino", Order = 2, ParentId = 18
                    },
                    new Category {
                        Id = 22, Name = "Dior", Order = 3, ParentId = 18
                    },
                    new Category {
                        Id = 23, Name = "Versace", Order = 4, ParentId = 18
                    },
                    new Category {
                        Id = 24, Name = "Kids", Order = 3, ParentId = null
                    },
                    new Category {
                        Id = 25, Name = "Fashion", Order = 4, ParentId = null
                    },
                    new Category {
                        Id = 26, Name = "Households", Order = 5, ParentId = null
                    },
                    new Category {
                        Id = 27, Name = "Interiors", Order = 6, ParentId = null
                    },
                    new Category {
                        Id = 28, Name = "Clothing", Order = 7, ParentId = null
                    },
                    new Category {
                        Id = 29, Name = "Bags", Order = 8, ParentId = null
                    },
                    new Category {
                        Id = 30, Name = "Shoes", Order = 9, ParentId = null
                    }
                };
                using var trans = context.Database.BeginTransaction();
                foreach (var section in categories)
                {
                    context.Categories.Add(section);
                }
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Categories] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Categories] OFF");
                trans.Commit();
            }

            // brands
            if (!context.Brands.Any())
            {
                var brands = new List <Brand>
                {
                    new Brand {
                        Id = 1, Name = "Acne", Order = 0
                    },
                    new Brand {
                        Id = 2, Name = "Grüne Erde", Order = 1
                    },
                    new Brand {
                        Id = 3, Name = "Albiro", Order = 2
                    },
                    new Brand {
                        Id = 4, Name = "Ronhill", Order = 3
                    },
                    new Brand {
                        Id = 5, Name = "Oddmolly", Order = 4
                    },
                    new Brand {
                        Id = 6, Name = "Boudestijn", Order = 5
                    },
                    new Brand {
                        Id = 7, Name = "Rösch creative culture", Order = 6
                    },
                };
                using var trans = context.Database.BeginTransaction();
                foreach (var brand in brands)
                {
                    context.Brands.Add(brand);
                }
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Brands] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Brands] OFF");
                trans.Commit();
            }

            // products
            if (!context.Products.Any())
            {
                var products = new List <Product>
                {
                    new Product()
                    {
                        Id = 1, Name = "Pixel Infrared Thermal Imager", Price = 1025, ImageUrl = "product1.jpg", Order = 0, CategoryId = 2, BrandId = 1
                    },
                    new Product()
                    {
                        Id = 2, Name = "Mini Electric Welding Machine", Price = 1025, ImageUrl = "product2.jpg", Order = 1, CategoryId = 2, BrandId = 1
                    },
                    new Product()
                    {
                        Id = 3, Name = "Digital Microscope", Price = 1025, ImageUrl = "product3.jpg", Order = 2, CategoryId = 2, BrandId = 1
                    },
                    new Product()
                    {
                        Id = 4, Name = "USB Digital Storage Oscilloscope", Price = 1025, ImageUrl = "product4.jpg", Order = 3, CategoryId = 2, BrandId = 1
                    },
                    new Product()
                    {
                        Id = 5, Name = "Intelligent 2 in 1 Digital", Price = 1025, ImageUrl = "product5.jpg", Order = 4, CategoryId = 2, BrandId = 2
                    },
                    new Product()
                    {
                        Id = 6, Name = "Spindle Motor", Price = 1025, ImageUrl = "product6.jpg", Order = 5, CategoryId = 2, BrandId = 2
                    },
                    new Product()
                    {
                        Id = 7, Name = "Digits LED Display", Price = 1025, ImageUrl = "product7.jpg", Order = 6, CategoryId = 2, BrandId = 2
                    },
                    new Product()
                    {
                        Id = 8, Name = "Digital Oscilloscope", Price = 1025, ImageUrl = "product8.jpg", Order = 7, CategoryId = 25, BrandId = 2
                    },
                    new Product()
                    {
                        Id = 9, Name = "HD Intelligent Graphical Digital Oscilloscope Multimeter", Price = 1025, ImageUrl = "product9.jpg", Order = 8, CategoryId = 25, BrandId = 2
                    },
                    new Product()
                    {
                        Id = 10, Name = "Cordless Brushless", Price = 1025, ImageUrl = "product10.jpg", Order = 9, CategoryId = 25, BrandId = 3
                    },
                    new Product()
                    {
                        Id = 11, Name = "Smart Laser Engraver DIY", Price = 1025, ImageUrl = "product11.jpg", Order = 10, CategoryId = 25, BrandId = 3
                    },
                    new Product()
                    {
                        Id = 12, Name = "Intelligent Solar Pure Sine Wave Inverter", Price = 1025, ImageUrl = "product12.jpg", Order = 11, CategoryId = 25, BrandId = 3
                    }
                };
                using var trans = context.Database.BeginTransaction();
                foreach (var product in products)
                {
                    context.Products.Add(product);
                }
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Products] ON");
                context.SaveChanges();
                context.Database.ExecuteSqlRaw(
                    "SET IDENTITY_INSERT [dbo].[Products] OFF");
                trans.Commit();
            }
        }
コード例 #13
0
        private static void InitProducts(WebStoreContext webStoreContext)
        {
            var products = new List <Product>
            {
                new Product
                {
                    Id         = 1,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product12.jpg",
                    Order      = 0,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product
                {
                    Id         = 2,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product11.jpg",
                    Order      = 1,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product
                {
                    Id         = 3,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product10.jpg",
                    Order      = 2,
                    CategoryId = 2,
                    BrandId    = 1
                },
                new Product
                {
                    Id         = 4,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product9.jpg",
                    Order      = 3,
                    CategoryId = 2,
                    BrandId    = 1,
                    IsNew      = true
                },
                new Product
                {
                    Id         = 5,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product8.jpg",
                    Order      = 4,
                    CategoryId = 2,
                    BrandId    = 2,
                    IsSale     = true
                },
                new Product
                {
                    Id         = 6,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product7.jpg",
                    Order      = 5,
                    CategoryId = 2,
                    BrandId    = 2
                },
                new Product
                {
                    Id         = 7,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product6.jpg",
                    Order      = 6,
                    CategoryId = 2,
                    BrandId    = 2
                },
                new Product
                {
                    Id         = 8,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product5.jpg",
                    Order      = 7,
                    CategoryId = 25,
                    BrandId    = 2
                },
                new Product
                {
                    Id         = 9,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product4.jpg",
                    Order      = 8,
                    CategoryId = 25,
                    BrandId    = 2
                },
                new Product
                {
                    Id         = 10,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product3.jpg",
                    Order      = 9,
                    CategoryId = 25,
                    BrandId    = 3
                },
                new Product
                {
                    Id         = 11,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product2.jpg",
                    Order      = 10,
                    CategoryId = 25,
                    BrandId    = 3
                },
                new Product
                {
                    Id         = 12,
                    Name       = "Easy Polo Black Edition",
                    Price      = 1025,
                    ImageUrl   = "product1.jpg",
                    Order      = 11,
                    CategoryId = 25,
                    BrandId    = 3
                }
            };

            using (var transaction = webStoreContext.Database.BeginTransaction())
            {
                foreach (var product in products)
                {
                    webStoreContext.Products.Add(product);
                }

                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] ON");
                webStoreContext.SaveChanges();
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Products] OFF");
                transaction.Commit();
            }
        }
コード例 #14
0
        private static void InitCategories(WebStoreContext webStoreContext)
        {
            var categories = new List <Category>
            {
                new Category
                {
                    Id       = 1,
                    Name     = "Sportswear",
                    Order    = 0,
                    ParentId = null
                },
                new Category
                {
                    Id       = 2,
                    Name     = "Nike",
                    Order    = 0,
                    ParentId = 1
                },
                new Category
                {
                    Id       = 3,
                    Name     = "Under Armour",
                    Order    = 1,
                    ParentId = 1
                },
                new Category
                {
                    Id       = 4,
                    Name     = "Adidas",
                    Order    = 2,
                    ParentId = 1
                },
                new Category
                {
                    Id       = 5,
                    Name     = "Puma",
                    Order    = 3,
                    ParentId = 1
                },
                new Category
                {
                    Id       = 6,
                    Name     = "ASICS",
                    Order    = 4,
                    ParentId = 1
                },
                new Category
                {
                    Id       = 7,
                    Name     = "Mens",
                    Order    = 1,
                    ParentId = null
                },
                new Category
                {
                    Id       = 8,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 9,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 10,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 11,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 12,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 13,
                    Name     = "Armani",
                    Order    = 5,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 14,
                    Name     = "Prada",
                    Order    = 6,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 15,
                    Name     = "Dolce and Gabbana",
                    Order    = 7,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 16,
                    Name     = "Chanel",
                    Order    = 8,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 17,
                    Name     = "Gucci",
                    Order    = 8,
                    ParentId = 7
                },
                new Category
                {
                    Id       = 18,
                    Name     = "Womens",
                    Order    = 2,
                    ParentId = null
                },
                new Category
                {
                    Id       = 19,
                    Name     = "Fendi",
                    Order    = 0,
                    ParentId = 18
                },
                new Category
                {
                    Id       = 20,
                    Name     = "Guess",
                    Order    = 1,
                    ParentId = 18
                },
                new Category
                {
                    Id       = 21,
                    Name     = "Valentino",
                    Order    = 2,
                    ParentId = 18
                },
                new Category
                {
                    Id       = 22,
                    Name     = "Dior",
                    Order    = 3,
                    ParentId = 18
                },
                new Category
                {
                    Id       = 23,
                    Name     = "Versace",
                    Order    = 4,
                    ParentId = 18
                },
                new Category
                {
                    Id       = 24,
                    Name     = "Kids",
                    Order    = 3,
                    ParentId = null
                },
                new Category
                {
                    Id       = 25,
                    Name     = "Fashion",
                    Order    = 4,
                    ParentId = null
                },
                new Category
                {
                    Id       = 26,
                    Name     = "Households",
                    Order    = 5,
                    ParentId = null
                },
                new Category
                {
                    Id       = 27,
                    Name     = "Interiors",
                    Order    = 6,
                    ParentId = null
                },
                new Category
                {
                    Id       = 28,
                    Name     = "Clothing",
                    Order    = 7,
                    ParentId = null
                },
                new Category
                {
                    Id       = 29,
                    Name     = "Bags",
                    Order    = 8,
                    ParentId = null
                },
                new Category
                {
                    Id       = 30,
                    Name     = "Shoes",
                    Order    = 9,
                    ParentId = null
                }
            };

            using (var transaction = webStoreContext.Database.BeginTransaction())
            {
                foreach (var category in categories)
                {
                    webStoreContext.Categories.Add(category);
                }

                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Categories] ON");
                webStoreContext.SaveChanges();
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Categories] OFF");
                transaction.Commit();
            }
        }
コード例 #15
0
        private static void InitBrands(WebStoreContext webStoreContext)
        {
            var brands = new List <Brand>
            {
                new Brand
                {
                    Id    = 1,
                    Name  = "Acne",
                    Order = 0
                },
                new Brand
                {
                    Id    = 2,
                    Name  = "Grüne Erde",
                    Order = 1
                },
                new Brand
                {
                    Id    = 3,
                    Name  = "Albiro",
                    Order = 2
                },
                new Brand
                {
                    Id    = 4,
                    Name  = "Ronhill",
                    Order = 3
                },
                new Brand
                {
                    Id    = 5,
                    Name  = "Oddmolly",
                    Order = 4
                },
                new Brand
                {
                    Id    = 6,
                    Name  = "Boudestijn",
                    Order = 5
                },
                new Brand
                {
                    Id    = 7,
                    Name  = "Rösch creative culture",
                    Order = 6
                }
            };

            using (var transaction = webStoreContext.Database.BeginTransaction())
            {
                foreach (var brand in brands)
                {
                    webStoreContext.Brands.Add(brand);
                }

                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] ON");
                webStoreContext.SaveChanges();
                webStoreContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Brands] OFF");
                transaction.Commit();
            }
        }
コード例 #16
0
 public ProductData(WebStoreContext context)
 {
     _context = context;
 }