コード例 #1
0
        public void seed()
        {
            //Certifica que o bd existe antes de qualquer coisa: Senão irá criá-lo.
            context.Database.EnsureCreated();

            if (!context.Products.Any())
            {
                //Precisa inserir dados
                //Pega um arquivo json e convert em objetos
                var filePath = Path.Combine(hosting.ContentRootPath, "Data/art.json");
                var json     = File.ReadAllText(filePath);
                var products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);

                //Adiciona a collection de products no banco
                context.AddRange(products);
                context.SaveChanges();

                //Pega a ordem já cadastrada pela migrations e cria uma orderItem
                var order = context.Orders.Where(o => o.Id == 1).FirstOrDefault();
                if (order != null)
                {
                    order.Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    };
                }
            }
        }
コード例 #2
0
 public bool SaveAll()
 {
     return(context.SaveChanges() > 0);
 }