public void Seed()
 {
     _ctx.Database.EnsureCreated();
     if (!_ctx.ProductTypes.Any())
     {
         var filePath     = Path.Combine(_env.ContentRootPath, "Setup/seedProductTypes.json");
         var json         = File.ReadAllText(filePath);
         var productTypes = JsonSerializer.Deserialize <IEnumerable <ProductType> >(json);
         if (productTypes != null)
         {
             _ctx.ProductTypes.AddRange(productTypes);
             _ctx.SaveChanges();
         }
     }
     if (!_ctx.Shops.Any())
     {
         var filePath = Path.Combine(_env.ContentRootPath, "Setup/seedShops.json");
         var json     = File.ReadAllText(filePath);
         var shops    = JsonSerializer.Deserialize <IEnumerable <Shop> >(json);
         if (shops != null)
         {
             _ctx.Shops.AddRange(shops);
             _ctx.SaveChanges();
         }
     }
     if (!_ctx.Products.Any())
     {
         var filePath = Path.Combine(_env.ContentRootPath, "Setup/seedProducts.json");
         var json     = File.ReadAllText(filePath);
         var products = JsonSerializer.Deserialize <IEnumerable <Product> >(json);
         if (products != null)
         {
             _ctx.Products.AddRange(products);
             _ctx.SaveChanges();
         }
     }
 }
Ejemplo n.º 2
0
        private void DatabaseSeed()
        {
            using (MyShopContext db = new MyShopContext())
            {
                db.Categories.Add(new Category()
                {
                    CategoryName = "Electronics"
                });
                db.Products.Add(new Product()
                {
                    ProductName = "SimpleProduct"
                });

                db.SaveChanges();
            }
        }