Exemplo n.º 1
0
        public void Seed()
        {
            _ctx.Database.EnsureCreated();

            if (!_ctx.Products.Any())
            {
                //need create sample data
                var filePath = Path.Combine(_hosting.ContentRootPath, "Models/art.json");
                var json     = File.ReadAllText(filePath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                _ctx.Products.AddRange(products);

                var order = _ctx.Orders.Where(o => o.Id == 2).FirstOrDefault();

                if (order != null)
                {
                    order.Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    };
                }


                _ctx.SaveChanges();
            }
        }
Exemplo n.º 2
0
 public bool SaveAll()
 {
     return(_context.SaveChanges() > 0);
 }