Ejemplo n.º 1
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                //to populate ProductBrand table with Seeds Data
                if (!context.ProductBrands.Any())
                {
                    var brandsData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var brands     = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);
                    brands.ForEach(item => { context.ProductBrands.Add(item); });
                    await context.SaveChangesAsync();
                }

                //to populate ProductType table with Seeds Data
                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
                    var types     = JsonSerializer.Deserialize <List <ProductType> >(typesData);
                    types.ForEach(item => { context.ProductTypes.Add(item); });
                    await context.SaveChangesAsync();
                }

                //to populate Products table with Seeds Data
                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
                    var products     = JsonSerializer.Deserialize <List <Product> >(productsData);
                    products.ForEach(item => { context.Products.Add(item); });
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 2
0
        public static async Task SeedAsync(StoreContext context)
        {
            try
            {
                WriteDataToDbSet <ProductBrand>(context);
                WriteDataToDbSet <ProductType>(context);
                WriteDataToDbSet <Product>(context);

                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
        private static async Task AddProducts(StoreContext storeContext)
        {
            if (!storeContext.Products.Any())
            {
                var productsData = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
                var products     = JsonSerializer.Deserialize <List <Product> >(productsData);

                foreach (var item in products)
                {
                    storeContext.Products.Add(item);
                }

                await storeContext.SaveChangesAsync();
            }
        }
Ejemplo n.º 4
0
        private static async Task RetrieveDataAsync <T>(StoreContext context, string jsonFile) where T : class
        {
            var entity = context.Set <T>();

            if (!entity.Any())
            {
                using (Stream stream = new FileStream(jsonFile, FileMode.Open))
                {
                    IEnumerable <T> data = await JsonSerializer.DeserializeAsync <IEnumerable <T> >(stream);

                    await entity.AddRangeAsync(data);

                    await context.SaveChangesAsync();
                }
            }
        }
Ejemplo n.º 5
0
        public static async Task SeedDataAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                CheckForData <ProductBrand>(context, !context.ProductBrands.Any(), "../Infrastructure/Data/SeedData/brands.json");
                CheckForData <ProductType>(context, !context.ProductBrands.Any(), "../Infrastructure/Data/SeedData/types.json");
                CheckForData <Product>(context, !context.ProductBrands.Any(), "../Infrastructure/Data/SeedData/products.json");

                await context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(e.Message);
            }
        }
Ejemplo n.º 6
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.Products.Any())
                {
                    var productData = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <IReadOnlyList <Product> >(productData);

                    foreach (var product in products)
                    {
                        await context.Products.AddAsync(product);
                    }
                }

                if (!context.ProductBrands.Any())
                {
                    var brandstData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <IReadOnlyList <ProductBrand> >(brandstData);

                    foreach (var brand in brands)
                    {
                        await context.ProductBrands.AddAsync(brand);
                    }
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <IReadOnlyList <ProductType> >(typesData);

                    foreach (var type in types)
                    {
                        await context.ProductTypes.AddAsync(type);
                    }
                }
                await context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 7
0
        public async Task SeedAsync()
        {
            var logger = _loggerFactory.CreateLogger <StoreContextSeed>();

            try
            {
                await SeedProducts();
                await SeedProductBrands();
                await SeedProductTypes();

                await _context.SaveChangesAsync();

                logger.LogInformation("SeedData is success.");
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "SeedData is failed.");
            }
        }
Ejemplo n.º 8
0
        public static async Task CreateStoreContextSeed(StoreContext dbcontext, ILoggerFactory loggerFactory)
        {
            try
            {
                //if(!dbcontext.ProductBrands.Any())
                //{
                //    var JsonBrand = File.ReadAllText("../Infrastructure/SeedData/brands.json");
                //    var productBrands = JsonSerializer.Deserialize<List<ProductBrand>>(JsonBrand);
                //    foreach (var brand in productBrands)
                //    {
                //        dbcontext.ProductBrands.Add(brand);
                //    }
                //    await dbcontext.SaveChangesAsync();
                //}

                //if(!dbcontext.ProductTypes.Any())
                //{
                //    var JsonType = File.ReadAllText("../Infrastructure/SeedData/types.json");
                //    var productType = JsonSerializer.Deserialize<List<ProductType>>(JsonType);
                //    foreach (var type in productType)
                //    {
                //        dbcontext.ProductTypes.Add(type);
                //    }
                //    await dbcontext.SaveChangesAsync();
                //}

                if (!dbcontext.Products.Any())
                {
                    var JsonProduct = File.ReadAllText("../Infrastructure/SeedData/products.json");
                    var products    = JsonSerializer.Deserialize <List <Product> >(JsonProduct);
                    foreach (var product in products)
                    {
                        dbcontext.Products.Add(product);
                    }
                    await dbcontext.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex, "An error occur when migration");
            }
        }
Ejemplo n.º 9
0
        public async void SeedContext()
        {
            try
            {
                if (!_context.Products.Any())
                {
                    var productsData = File.ReadAllText("../Infrastructure/Data/json/products.json");
                    var products     = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var product in products)
                    {
                        _context.Products.Add(product);
                    }

                    await _context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var log = _logger.CreateLogger <SeedStoreContext>();
                log.LogError(ex.Message);
            }
        }
Ejemplo n.º 10
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.Bugs.Any())
                {
                    var bugsData = File.ReadAllText("../Infrastructure/Data/SeedData/bugs.json");

                    var bugs = JsonSerializer.Deserialize <List <Bug> >(bugsData);

                    foreach (var item in bugs)
                    {
                        context.Bugs.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 11
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try{
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                if (!context.ProductBrands.Any())
                {
                    var brandsData = File.ReadAllText(path + @"../../../../../Infrastructure/Data/SeedData/brands.json");



                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                        context.Database.OpenConnection();
                        try
                        {
                            context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.ProductBrands ON");
                            context.SaveChanges();
                            context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.ProductBrands OFF");
                        }
                        finally
                        {
                            context.Database.CloseConnection();
                        }
                    }

                    await context.SaveChangesAsync();
                }
                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText(path + @"../../../../../Infrastructure/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                        context.Database.OpenConnection();
                        try
                        {
                            context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.ProductTypes ON");
                            context.SaveChanges();
                            context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT dbo.ProductTypes OFF");
                        }
                        finally
                        {
                            context.Database.CloseConnection();
                        }
                    }

                    await context.SaveChangesAsync();
                }
                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText(path + @"../../../../../Infrastructure/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                        // context.Database.OpenConnection();
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 12
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                var path = @"C:\Users\Denise\Documents\projects\Skinet\Infrastructure\Data\";

                if (!context.ProductBrands.Any())
                {
                    var brandsData =
                        File.ReadAllText(path + @"\SeedData\brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData =
                        File.ReadAllText(path + @"\SeedData\types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData =
                        File.ReadAllText(path + @"\SeedData\products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        Guid newId = Guid.NewGuid();
                        item.Id = newId;
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                //if (!context.DeliveryMethods.Any())
                //{
                //    var dmData =
                //        File.ReadAllText(path + @"\SeedData\delivery.json");

                //    var methods = JsonSerializer.Deserialize<List<DeliveryMethod>>(dmData);

                //    foreach (var item in methods)
                //    {
                //        context.DeliveryMethods.Add(item);
                //    }

                //    await context.SaveChangesAsync();
                //}
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 13
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    var brandsData = File.ReadAllText(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText(path + @"/Data/SeedData/types.json");
                    var types     = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText(path + @"/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    products.ForEach(item => context.Products.Add(item));

                    // foreach(var item in products)
                    // {
                    //     context.Products.Add(item);
                    // }
                    await context.SaveChangesAsync();
                }

                // 210. data to be seeded for DeliveryMethods
                // -> add migration next.
                // 211.
                // dotnet ef migrations add OrderEntityAdded -p Infrastructure -s API -c StoreContext
                if (!context.DeliveryMethods.Any())
                {
                    var dmData = File.ReadAllText(path + @"/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);


                    foreach (var item in methods)
                    {
                        methods.ForEach(item => context.DeliveryMethods.Add(item));
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 14
0
        public async Task <int> SaveAsync(Product productDetails)
        {
            await _context.AddAsync(productDetails);

            return(await _context.SaveChangesAsync());
        }
Ejemplo n.º 15
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                context.Products.Include("");
                // var data = context.Products.Include(p => p.ProductSizeAndQuantity)
                // .ThenInclude(s => s.Size)
                // .FirstOrDefault();
                // var logger = loggerFactory.CreateLogger("ProductQuantityAndSize");
                // logger.LogCritical(data.ProductSizeAndQuantity.FirstOrDefault().Size.SizeShortName.ToString());

                if (!context.ProductTypes.Any())
                {
                    var typesData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);
                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.Sizes.Any())
                {
                    var sizeData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/ProductSizes.json");
                    var sizes = JsonSerializer.Deserialize <List <Size> >(sizeData);
                    foreach (var size in sizes)
                    {
                        context.Sizes.Add(size);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.ProductGenderBase.Any())
                {
                    var brandsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/GenderBase.json");
                    var brands = JsonSerializer.Deserialize <List <ProductGenderBase> >(brandsData);
                    foreach (var item in brands)
                    {
                        context.ProductGenderBase.Add(item);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");
                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);
                    foreach (var item in methods)
                    {
                        context.DeliveryMethods.Add(item);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);
                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }
                    await context.SaveChangesAsync();
                }
                //Seed all product Quantity Sizes with random quantity number
                if (!context.ProductSizeAndQuantity.Any())
                {
                    foreach (var product in context.Products.ToArray())
                    {
                        var productSizesForTheType = context.Sizes.Where(s => s.ProductTypeId == product.ProductTypeId).ToArray();

                        for (int i = 0; i < productSizesForTheType.Length; i++)
                        {
                            var randomQuantity = new Random();

                            var productQuantitySizeData = new ProductSizeAndQuantity()
                            {
                                ProductId = product.Id,
                                SizeId    = productSizesForTheType[i].Id,
                                Quantity  = randomQuantity.Next(1, 100)
                            };

                            context.ProductSizeAndQuantity.Add(productQuantitySizeData);
                        }

                        context.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 16
0
        public static async Task Initialize(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.ProductBrands.Any())
                {
                    context.ProductBrands.AddRange(
                        new ProductBrand
                    {
                        Name = "Adidas",
                    },

                        new ProductBrand
                    {
                        Name = "Nike",
                    },

                        new ProductBrand
                    {
                        Name = "Pumma",
                    }
                        );

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    context.ProductTypes.AddRange(
                        new ProductType
                    {
                        Name = "Shoes",
                    },

                        new ProductType
                    {
                        Name = "T-Shirt",
                    },

                        new ProductType
                    {
                        Name = "Sweat suit",
                    }
                        );

                    await context.SaveChangesAsync();
                }

                if (!context.Images.Any())
                {
                    context.Images.Add(new Image
                    {
                        Alt      = "No image",
                        Location = "~/Media/Images/Product/noimage.png"
                    });

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    context.Products.AddRange(
                        new Product
                    {
                        Name           = "T-Shirt 1",
                        Description    = "Just a regular T-shirt",
                        Price          = 2.20M,
                        Barcode        = "901237128931",
                        Quantity       = 3,
                        ProductTypeId  = 2,
                        ProductBrandId = 2
                    },
                        new Product
                    {
                        Name           = "Shoe 1",
                        Description    = "Just a regular shoe",
                        Price          = 2.90M,
                        Barcode        = "89123jhasdas",
                        Quantity       = 4,
                        ProductTypeId  = 1,
                        ProductBrandId = 1
                    },
                        new Product
                    {
                        Name           = "Sweat suit 1",
                        Description    = "Just a regular Sweat suit",
                        Price          = 1.80M,
                        Quantity       = 9,
                        Barcode        = "218372jkmasidi",
                        ProductTypeId  = 3,
                        ProductBrandId = 3
                    }
                        );

                    await context.SaveChangesAsync();
                }

                if (!context.ProductImages.Any())
                {
                    context.ProductImages.AddRange(
                        new ProductImage
                    {
                        ProductId = 1,
                        ImageId   = 1
                    },

                        new ProductImage
                    {
                        ProductId = 2,
                        ImageId   = 1
                    },

                        new ProductImage
                    {
                        ProductId = 3,
                        ImageId   = 1
                    }
                        );

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <SeedData>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 17
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.ProductBrands.Any())
                {
                    /// await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductBrands ON");
                    // using var transaction = context.Database.BeginTransaction();
                    var brandsData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var brands     = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);
                    //context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ProductBrands ON");

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }
                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT ProductBrands ON");
                    await context.SaveChangesAsync();

                    context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT ProductBrands OFF");
                    //context.Database.ExecuteSqlCommand("SET IDENTITY_INSERT ProductBrands OFF");
                    //await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductBrands OFF");
                    //transaction.Commit();
                }

                if (!context.ProductTypes.Any())
                {
                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductTypes ON");

                    var typesdata = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
                    var types     = JsonSerializer.Deserialize <List <ProductType> >(typesdata);
                    //context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT ProductTypes ON");

                    foreach (var type in types)
                    {
                        context.ProductTypes.Add(type);
                    }

                    await context.SaveChangesAsync();

                    //context.Database.ExecuteSqlRaw("SET IDENTITY_INSERT ProductTypes OFF");
                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductTypes OFF");
                }

                if (!context.Products.Any())
                {
                    var productdata = File.ReadAllText("../Infrastructure/Data/SeedData/products.json");
                    var products    = JsonSerializer.Deserialize <List <Product> >(productdata);

                    foreach (var pro in products)
                    {
                        context.Products.Add(pro);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 18
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                // var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    var brandsData =
                        File.ReadAllText(@"C:\Users\dellxps\Desktop\MHS_IT\Proj\PJECOM\NetEcom\Infrastructure\Data\SeedData\brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData =
                        File.ReadAllText(@"C:\Users\dellxps\Desktop\MHS_IT\Proj\PJECOM\NetEcom\Infrastructure\Data\SeedData\types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData =
                        File.ReadAllText(@"C:\Users\dellxps\Desktop\MHS_IT\Proj\PJECOM\NetEcom\Infrastructure\Data\SeedData\products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                // if (!context.DeliveryMethods.Any())
                // {
                //     var dmData =
                //         File.ReadAllText(path + @"/Data/SeedData/delivery.json");

                //     var methods = JsonSerializer.Deserialize<List<DeliveryMethod>>(dmData);

                //     foreach (var item in methods)
                //     {
                //         context.DeliveryMethods.Add(item);
                //     }

                //     await context.SaveChangesAsync();
                // }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 19
0
 public async Task<int> Complete() {
     return await _storeContext.SaveChangesAsync();
 }
Ejemplo n.º 20
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try {
                // Делаем так, чтобы можно было опубликовать приложение -
                // Это также работает в разработке, потому что в Infra.csproj добавлен параметр None.
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    // var brandsData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var brandsData = File.ReadAllText(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText(path + @"/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText(path + @"/Data/SeedData/products.json");

                    // Product.cs does net have a pictureUrl anymore
                    var products = JsonSerializer.Deserialize <List <ProductSeedModel> >(productsData);

                    foreach (var item in products)
                    {
                        var pictureFileName = item.PictureUrl.Substring(16);
                        var product         = new Product
                        {
                            Name           = item.Name,
                            Description    = item.Description,
                            Price          = item.Price,
                            ProductBrandId = item.ProductBrandId,
                            ProductTypeId  = item.ProductTypeId
                        };
                        product.AddPhoto(item.PictureUrl, pictureFileName);
                        context.Products.Add(product);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData = File.ReadAllText(path + @"/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var item in methods)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            } catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 21
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                // var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    //var brandsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/brands.json");
                    var brandsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    //var typesData =
                    //    File.ReadAllText(path + @"/Data/SeedData/types.json");
                    var typesData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    //var productsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                    var productsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }



                //if (context.Categorys.Count() < 2)
                //{
                //    //var productsData =
                //    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                //    var categorysData =
                //        File.ReadAllText("../Infrastructure/Data/SeedNewData/categories.json");

                //    var categorys = JsonSerializer.Deserialize<List<Category>>(categorysData);

                //    foreach (var item in categorys)
                //    {
                //        Category category = setCategory(context, item);

                //        context.Categorys.Add(category);
                //    }

                //    await context.SaveChangesAsync();
                //}

                if (context.Items.Count() < 31)
                {
                    //var productsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                    var itemsData =
                        File.ReadAllText("../Infrastructure/Data/SeedNewData/items.json");

                    var items = JsonSerializer.Deserialize <List <Item> >(itemsData);

                    foreach (var item in items)
                    {
                        context.Items.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Colors.Any())
                {
                    //var productsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                    var colorsData =
                        File.ReadAllText("../Infrastructure/Data/SeedNewData/colors.json");

                    var colors = JsonSerializer.Deserialize <List <Color> >(colorsData);

                    foreach (var item in colors)
                    {
                        context.Colors.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Sizes.Any())
                {
                    //var productsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                    var sizesData =
                        File.ReadAllText("../Infrastructure/Data/SeedNewData/sizes.json");

                    var sizes = JsonSerializer.Deserialize <List <Size> >(sizesData);

                    foreach (var item in sizes)
                    {
                        context.Sizes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (context.Images.Count() < 49)
                {
                    //var productsData =
                    //    File.ReadAllText(path + @"/Data/SeedData/products.json");'
                    var imagesData =
                        File.ReadAllText("../Infrastructure/Data/SeedNewData/images.json");

                    var images = JsonSerializer.Deserialize <List <Image> >(imagesData);

                    foreach (var item in images)
                    {
                        context.Images.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
                if (!context.DeliveryMethods.Any())
                {
                    //var dmData =
                    //    File.ReadAllText(path + @"/Data/SeedData/delivery.json");
                    var dmData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var item in methods)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 22
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.ProductBrands.Any())
                {
                    var brandsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");

                    var deliveryMethods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var item in deliveryMethods)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }


                if (!context.ProductTypes.Any())
                {
                    var typesData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
                if (!context.Products.Any())
                {
                    var productsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 23
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    var brandsData = await File.ReadAllTextAsync(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var brand in brands)
                    {
                        context.ProductBrands.Add(brand);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData = await File.ReadAllTextAsync(path + @"/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var type in types)
                    {
                        context.ProductTypes.Add(type);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData = await File.ReadAllTextAsync(path + @"/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <ProductSeedModel> >(productsData);

                    foreach (var item in products)
                    {
                        var pictureFileName = item.PictureUrl.Substring(16);
                        var product         = new Product
                        {
                            Name           = item.Name,
                            Description    = item.Description,
                            Price          = item.Price,
                            ProductBrandId = item.ProductBrandId,
                            ProductTypeId  = item.ProductTypeId
                        };

                        product.AddPhoto(item.PictureUrl, pictureFileName);
                        context.Products.Add(product);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData = await File.ReadAllTextAsync(path + @"/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var item in methods)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (System.Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 24
0
        private static async Task TheOrientExpress(StoreContext context)
        {
            if (await context.Games.AnyAsync())
            {
                return;
            }

            Game game = new Game
            {
                Name               = "The Oriënt Express",
                ResourceUrl        = "games/the_orient_express/logo.png",
                CompletedGameStage = 100,
            };

            var dialogues = new List <Dialogue>
            {
                new Dialogue
                {
                    Id       = 1,
                    Messages = "Bonjour Detective.;The name is Hercule Poirot.;It is a pleasure to meet you."
                },
                new Dialogue
                {
                    Id       = 2,
                    Messages = "Hi"
                },
            };

            context.Games.Add(game);
            context.Dialogues.AddRange(dialogues);
            await context.SaveChangesAsync();

            game.Characters = new List <Character>
            {
                new Character
                {
                    GameId             = game.Id,
                    Name               = "Lieutenant Morris",
                    ResourceUrl        = "games/the_orient_express/lieutenant_morris.png",
                    CharacterDialogues = new List <CharacterDialogue>
                    {
                        new CharacterDialogue
                        {
                            DialogueId     = 2,
                            CharacterStage = 0,
                        }
                    }
                }
            };

            game.GameDialogues = new List <GameDialogue>
            {
                new GameDialogue
                {
                    DialogueId          = 1,
                    GameId              = game.Id,
                    GameStage           = 0,
                    GameDialogueActions = new List <GameDialogueAction>
                    {
                        new GameDialogueAction
                        {
                            Action        = "The pleasure is all mine.What has happened?",
                            NextGameStage = 1
                        }
                    }
                }
            };
            game.Hints = new List <Hint>
            {
                new Hint
                {
                    GameId      = game.Id,
                    GameStage   = 1,
                    Information = "That's it folks"
                }
            };

            context.Games.Update(game);
            await context.SaveChangesAsync();
        }
Ejemplo n.º 25
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            // We need to add try/catch block because, the expection throw is only in program.cs class.
            try
            {
                if (!context.ProductBrands.Any())
                {
                    var brandsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");

                    var brands = JsonConvert.DeserializeObject <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/types.json");

                    var types = JsonConvert.DeserializeObject <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/products.json");

                    var products = JsonConvert.DeserializeObject <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData =
                        File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");

                    var delivery = JsonConvert.DeserializeObject <List <DeliveryMethod> >(dmData);

                    foreach (var item in delivery)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContext>();
                logger.LogError(ex, "Error has SEEEEEDIIIIIINGGGG");
            }
        }
Ejemplo n.º 26
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    // Will be run from Program Class path.
                    // var brandsData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var brandsData = File.ReadAllText(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);


                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.Database.OpenConnectionAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductBrands ON");

                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductBrands OFF");

                    context.Database.CloseConnection();
                }

                if (!context.ProductTypes.Any())
                {
                    // Will be run from Program Class path.
                    var typesData = File.ReadAllText(path + @"/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.Database.OpenConnectionAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductTypes ON");

                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT ProductTypes OFF");

                    context.Database.CloseConnection();
                }

                if (!context.Products.Any())
                {
                    // Will be run from Program Class path.
                    var productData = File.ReadAllText(path + @"/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    // Will be run from Program Class path.
                    var dmData = File.ReadAllText(path + @"/Data/SeedData/delivery.json");

                    var deliveryMethods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var method in deliveryMethods)
                    {
                        context.DeliveryMethods.Add(method);
                    }

                    await context.Database.OpenConnectionAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT DeliveryMethods ON");

                    await context.SaveChangesAsync();

                    await context.Database.ExecuteSqlRawAsync("SET IDENTITY_INSERT DeliveryMethods OFF");

                    context.Database.CloseConnection();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();

                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 27
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductBrands.Any())
                {
                    var brandsData = File.ReadAllText(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    var typesData = File.ReadAllText(path + @"/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    var productsData = File.ReadAllText(path + @"/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var dmData = File.ReadAllText(path + @"/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    foreach (var item in methods)
                    {
                        context.DeliveryMethods.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }

            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 28
0
        public static async Task SeedAsync(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                // 3 if statements to get the json from our 3 json files to
                // get the data into the database for the site
                if (!context.ProductBrands.Any())
                {
                    // seeding Data then serializing it to be hosted/navigatable via website?
                    // productbrands, product types and product list
                    var brandsData =
                        File.ReadAllText(path + @"/Data/SeedData/brands.json");

                    var brands = JsonSerializer.Deserialize <List <ProductBrand> >(brandsData);

                    foreach (var item in brands)
                    {
                        context.ProductBrands.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.ProductTypes.Any())
                {
                    // seeding Data then serializing it to be hosted/navigatable via website?
                    // productbrands, product types and product list
                    var typesData =
                        File.ReadAllText(path + @"/Data/SeedData/types.json");

                    var types = JsonSerializer.Deserialize <List <ProductType> >(typesData);

                    foreach (var item in types)
                    {
                        context.ProductTypes.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Products.Any())
                {
                    // seeding Data then serializing it to be hosted/navigatable via website?
                    // productbrands, product types and product list
                    var productsData =
                        File.ReadAllText(path + @"/Data/SeedData/products.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productsData);

                    foreach (var item in products)
                    {
                        context.Products.Add(item);
                    }

                    await context.SaveChangesAsync();
                }
            }

            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }
Ejemplo n.º 29
0
 public async Task <int> Complete()
 {
     return(await _context.SaveChangesAsync());
 }
Ejemplo n.º 30
0
        public static async Task SeedAsync(
            StoreContext context,
            ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.ProductCategories.Any())
                {
                    var categoryData = File.ReadAllText("../Infrastructure/Data/SeedData/ProductCategories_json.json");

                    var categories = JsonSerializer.Deserialize <List <ProductCategory> >(categoryData);

                    try
                    {
                        foreach (var item in categories)
                        {
                            context.ProductCategories.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductCategories ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductCategories OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.Products.Any())
                {
                    var productData = File.ReadAllText("../Infrastructure/Data/SeedData/Products_json.json");

                    var products = JsonSerializer.Deserialize <List <Product> >(productData);

                    try
                    {
                        foreach (var item in products)
                        {
                            context.Products.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Products ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Products OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.Products_ProductCategories.Any())
                {
                    var product_CategoriesData = File.ReadAllText("../Infrastructure/Data/SeedData/Product_Categories__json.json");

                    var product_Categories = JsonSerializer.Deserialize <List <Product_ProductCategory> >(product_CategoriesData);

                    foreach (var item in product_Categories)
                    {
                        context.Products_ProductCategories.Add(item);
                    }

                    await context.SaveChangesAsync();
                }

                if (!context.Options.Any())
                {
                    var optionsData = File.ReadAllText("../Infrastructure/Data/SeedData/Options_json.json");

                    var options = JsonSerializer.Deserialize <List <Option> >(optionsData);

                    try
                    {
                        foreach (var item in options)
                        {
                            context.Options.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Options ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Options OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.OptionValues.Any())
                {
                    var optionValuesData = File.ReadAllText("../Infrastructure/Data/SeedData/OptionValues_json.json");

                    var optionValues = JsonSerializer.Deserialize <List <OptionValue> >(optionValuesData);

                    try
                    {
                        foreach (var item in optionValues)
                        {
                            context.OptionValues.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.OptionValues ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.OptionValues OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.ProductVariantOptions.Any())
                {
                    var productVariantOptionsData = File.ReadAllText("../Infrastructure/Data/SeedData/ProductVariantOptions_json.json");

                    var productVariantOptions = JsonSerializer.Deserialize <List <ProductVariantOption> >(productVariantOptionsData);

                    try
                    {
                        foreach (var item in productVariantOptions)
                        {
                            context.ProductVariantOptions.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductVariantOptions ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductVariantOptions OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.ProductVariants.Any())
                {
                    var productVariantsData = File.ReadAllText("../Infrastructure/Data/SeedData/ProductVariants_json.json");

                    var productVariants = JsonSerializer.Deserialize <List <ProductVariant> >(productVariantsData);

                    try
                    {
                        foreach (var item in productVariants)
                        {
                            context.ProductVariants.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductVariants ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.ProductVariants OFF");

                        context.Database.CloseConnection();
                    }
                }

                if (!context.SKUs.Any())
                {
                    var skuData = File.ReadAllText("../Infrastructure/Data/SeedData/SKUs_json.json");

                    var Skus = JsonSerializer.Deserialize <List <SKU> >(skuData);

                    try
                    {
                        foreach (var item in Skus)
                        {
                            context.SKUs.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.SKUs ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.SKUs OFF");

                        context.Database.CloseConnection();
                    }
                }

                // ----------------------------------- //

                if (!context.DeliveryMethods.Any())
                {
                    var dmData = File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");

                    var methods = JsonSerializer.Deserialize <List <DeliveryMethod> >(dmData);

                    try
                    {
                        foreach (var item in methods)
                        {
                            context.DeliveryMethods.Add(item);
                            context.Database.OpenConnection();
                            await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.DeliveryMethods ON");

                            context.Add(item);
                        }
                    }
                    finally
                    {
                        await context.SaveChangesAsync();

                        await context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.DeliveryMethods OFF");

                        context.Database.CloseConnection();
                    }
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <StoreContextSeed>();
                logger.LogError(ex.Message);
            }
        }