コード例 #1
0
        public void Seed()
        {
            context.Database.EnsureCreated();

            if (!context.Products.Any())
            {
                //Need to Seed
                string filePath = Path.Combine(hosting.ContentRootPath, "Data/art.json");
                var    json     = File.ReadAllText(filePath);
                var    products = JsonConvert.DeserializeObject <IEnumerable <Product> >(json);
                context.Products.AddRange(products);

                var order = new Order()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "IN00001",
                    Items       = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                context.Orders.Add(order);

                context.SaveChanges();
            }
        }
コード例 #2
0
 public bool SaveAll()
 {
     return(context.SaveChanges() > 0);
     /*Note: context.SaveChanges() => returns number of saved rows */
 }