示例#1
0
        public void Seed()
        {
            ctx.Database.EnsureCreated();

            if (!ctx.ArtProducts.Any())
            {
                // Need to create sample data
                var filepath = Path.Combine(hosting.ContentRootPath, "Data/art.json");
                var json     = File.ReadAllText(filepath);
                var products = JsonConvert.DeserializeObject <IEnumerable <ArtProduct> >(json);
                ctx.ArtProducts.AddRange(products);

                var order = new ArtOrder()
                {
                    OrderDate   = DateTime.Now,
                    OrderNumber = "00001",
                    //User = user,
                    Items = new List <ArtOrderItem>()
                    {
                        new ArtOrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    }
                };

                ctx.ArtOrders.Add(order);

                ctx.SaveChanges();

                var orderCheck = ctx.ArtOrders.Where(o => o.Id == 1).FirstOrDefault();
                if (orderCheck != null)
                {
                    orderCheck.Items = new List <ArtOrderItem>()
                    {
                        new ArtOrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    };
                }

                ctx.SaveChanges();
            }
        }
 public bool SaveAll()
 {
     return(ctx.SaveChanges() > 0);
 }