コード例 #1
0
 public EcommerceRepository(EcommerceContext context)
 {
     _context = context;
 }
コード例 #2
0
 public UnitOfWork(EcommerceContext context)
 {
     _context = context;
 }
コード例 #3
0
        public static async Task SeedAsync(EcommerceContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.ProductBrands.Any())
                {
                    var brandsData = File.ReadAllText("/var/www/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("/var/www/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("/var/www/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 deliveryData = File.ReadAllText("/var/www/Infrastructure/Data/SeedData/delivery.json");

                    var DeliveryMethods = JsonSerializer.Deserialize <List <DeliveryMethod> >(deliveryData);

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

                    await context.SaveChangesAsync();
                }
            }
            catch (Exception ex)
            {
                var logger = loggerFactory.CreateLogger <EcommerceContextSeed>();
                logger.LogError(ex.Message);
            }
        }