コード例 #1
0
        /// <summary>
        /// Seeding method.
        /// </summary>
        /// <param name="dbContext">Database to seed in.</param>
        /// <param name="serviceProvider">Service provider.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (await dbContext.Categories.AnyAsync())
            {
                return;
            }

            int cafeteriaId = dbContext.CategoryImages
                              .FirstOrDefault(image => image.ImageUrl.Contains(CafeteriaName.ToSlug()))
                              .Id;
            int casualDiningId = dbContext.CategoryImages
                                 .FirstOrDefault(image => image.ImageUrl.Contains(CasualDiningName.ToSlug()))
                                 .Id;
            int ethnicId = dbContext.CategoryImages
                           .FirstOrDefault(image => image.ImageUrl.Contains(EthnicName.ToSlug()))
                           .Id;
            int familyStyleId = dbContext.CategoryImages
                                .FirstOrDefault(image => image.ImageUrl.Contains(FamilyStyleName.ToSlug()))
                                .Id;
            int fastFoodId = dbContext.CategoryImages
                             .FirstOrDefault(image => image.ImageUrl.Contains(FastFoodName.ToSlug()))
                             .Id;
            int fineDiningId = dbContext.CategoryImages
                               .FirstOrDefault(image => image.ImageUrl.Contains(FineDiningName.ToSlug()))
                               .Id;
            int premiumCasualId = dbContext.CategoryImages
                                  .FirstOrDefault(image => image.ImageUrl.Contains(PremiumCasualName.ToSlug()))
                                  .Id;
            int pubId = dbContext.CategoryImages
                        .FirstOrDefault(image => image.ImageUrl.Contains(PubName.ToSlug()))
                        .Id;

            List <(string Name, int ImageId, string Description)> categories
                = new List <(string Name, int ImageId, string Description)>
                {
                (CafeteriaName, cafeteriaId, CafeteriaDescription),
                (CasualDiningName, casualDiningId, CasualDiningDescription),
                (EthnicName, ethnicId, EthnicDescription),
                (FamilyStyleName, familyStyleId, FamilyStyleDescription),
                (FastFoodName, fastFoodId, FastFoodDescription),
                (FineDiningName, fineDiningId, FineDiningDescription),
                (PremiumCasualName, premiumCasualId, PremiumCasualDescription),
                (PubName, pubId, PubDescription),
                };

            foreach (var category in categories)
            {
                await dbContext.Categories.AddAsync(new Category
                {
                    Name        = category.Name,
                    Description = category.Description,
                    Title       = category.Name,
                    ImageId     = category.ImageId,
                });
            }
        }
    }
コード例 #2
0
        /// <summary>
        /// Seeding method.
        /// </summary>
        /// <param name="dbContext">Database.</param>
        /// <param name="serviceProvider">Service provider.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
        public async Task SeedAsync(ApplicationDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (await dbContext.CategoryImages.AnyAsync())
            {
                return;
            }

            var cafeteriaImage = await this.UploadImagesToCloudinaryAsync(Cafeteria, CafeteriaName.ToSlug());

            var casualImage = await this.UploadImagesToCloudinaryAsync(CasualDining, CasualDiningName.ToSlug());

            var ethnicImage = await this.UploadImagesToCloudinaryAsync(Ethnic, EthnicName.ToSlug());

            var familyImage = await this.UploadImagesToCloudinaryAsync(FamilyStyle, FamilyStyleName.ToSlug());

            var fastFoodImage = await this.UploadImagesToCloudinaryAsync(FastFood, FastFoodName.ToSlug());

            var fineImage = await this.UploadImagesToCloudinaryAsync(FineDining, FineDiningName.ToSlug());

            var premiumImage = await this.UploadImagesToCloudinaryAsync(PremiumCasual, PremiumCasualName.ToSlug());

            var pubImage = await this.UploadImagesToCloudinaryAsync(Pub, PubName.ToSlug());

            List <(string ImageUrl, string PublicId)> categoryImages
                = new List <(string ImageUrl, string PublicId)>
                {
                (cafeteriaImage.ImageUrl, cafeteriaImage.PublicId),
                (casualImage.ImageUrl, casualImage.PublicId),
                (ethnicImage.ImageUrl, ethnicImage.PublicId),
                (familyImage.ImageUrl, familyImage.PublicId),
                (fastFoodImage.ImageUrl, fastFoodImage.PublicId),
                (fineImage.ImageUrl, fineImage.PublicId),
                (premiumImage.ImageUrl, premiumImage.PublicId),
                (pubImage.ImageUrl, pubImage.PublicId),
                };

            foreach (var image in categoryImages)
            {
                await dbContext.CategoryImages.AddAsync(new CategoryImage
                {
                    ImageUrl = image.ImageUrl,
                    PublicId = image.PublicId,
                });
            }
        }