Ejemplo n.º 1
0
        public static async Task SeedDataAsync(SkinetContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                // var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

                if (!context.ProductTypes.Any())
                {
                    var productsTypeData = File.ReadAllText("../Infrastructure/Data/SeedData/types.json");
                    var productTypes     = JsonConvert.DeserializeObject <List <ProductType> >(productsTypeData);

                    foreach (var type in productTypes)
                    {
                        context.ProductTypes.Add(type);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.ProductBrands.Any())
                {
                    var productsBrandData = File.ReadAllText("../Infrastructure/Data/SeedData/brands.json");
                    var productBrands     = JsonConvert.DeserializeObject <List <ProductBrand> >(productsBrandData);

                    foreach (var brand in productBrands)
                    {
                        context.ProductBrands.Add(brand);
                    }
                    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 product in products)
                    {
                        context.Products.Add(product);
                    }
                    await context.SaveChangesAsync();
                }

                if (!context.DeliveryMethods.Any())
                {
                    var deliveryMethodData = File.ReadAllText("../Infrastructure/Data/SeedData/delivery.json");
                    var deliveryMethods    = JsonConvert.DeserializeObject <List <DeliveryMethod> >(deliveryMethodData);

                    foreach (var deliveryMethod in deliveryMethods)
                    {
                        context.DeliveryMethods.Add(deliveryMethod);
                    }
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <SeedDataContext>();
                logger.LogError("An error ocured when seeding data", ex);
            }
        }
Ejemplo n.º 2
0
        public static async Task SeedAsync(SkinetContext ctx, ILoggerFactory logFactory)
        {
            try
            {
                if (!ctx.ProductBrands.Any())
                {
                    var brandsData = await File.ReadAllTextAsync("../Infrastructure/Data/SeedData/brands.json");

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

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

                    await ctx.SaveChangesAsync();
                }

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

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

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

                    await ctx.SaveChangesAsync();
                }

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

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

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

                    await ctx.SaveChangesAsync();
                }
            }
            catch (Exception e)
            {
                var logger = logFactory.CreateLogger <SkinetContextSeed>();
                logger.LogError(e.Message);
            }
        }
Ejemplo n.º 3
0
 public async Task <int> Complete()
 {
     return(await _ctx.SaveChangesAsync());
 }