Exemple #1
0
        private void GetProducts(Models.ApiContext context)
        {
            // var names = assembly.GetManifestResourceNames();
            var resourceName = "API.products.json";
            var jsonData     = GetEmbeddedResourceAsString(resourceName);

            var productsSeed = JsonConvert.DeserializeObject <ICollection <ProductSeed> >(jsonData);

            var categories = productsSeed
                             .GroupBy(p => p.Category)
                             .Select(c => new Category
            {
                Name     = c.Key,
                Products = new List <Product>()
            });

            context.Categories.AddOrUpdate(categories.ToArray());
            context.SaveChanges();

            foreach (var productToSeed in productsSeed)
            {
                var category = context.Categories.FirstOrDefault(c => c.Name == productToSeed.Category);
                context.Products.AddOrUpdate(new Product
                {
                    Name        = productToSeed.Name,
                    Description = productToSeed.Description,
                    Category    = category
                });
            }
        }
Exemple #2
0
 protected override void Seed(Models.ApiContext context)
 {
     GetProducts(context);
     context.SaveChanges();
 }