예제 #1
0
 public void CreateTiger(TigerModel model)
 {
     using (var context = new TigerContext()) {
         var entity = new Tiger
         {
             Id     = Guid.NewGuid(),
             Color  = model.Color,
             Weight = model.Weight
         };
         context.Tigers.Add(entity);
         context.SaveChanges();
     }
 }
예제 #2
0
        public async Task SeedAsync()
        {
            _ctx.Database.EnsureCreated();
            StoreUser user = await _userManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                user = new StoreUser()
                {
                    FristName = "Hil",
                    LastName  = "W",
                    Email     = "*****@*****.**",
                    UserName  = "******"
                };
                var result = await _userManager.CreateAsync(user, "PX9XW@@rd");

                if (result != IdentityResult.Success)
                {
                    throw new InvalidOperationException("Could not create new user in Seeder.");
                }
            }



            if (!_ctx.Products.Any())
            {
                var filepath = Path.Combine(_hosting.ContentRootPath, "Data/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 == 1).FirstOrDefault();
                if (order != null)
                {
                    order.User  = user; //this updates the order to be owned by the user
                    order.Items = new List <OrderItem>()
                    {
                        new OrderItem()
                        {
                            Product   = products.First(),
                            Quantity  = 5,
                            UnitPrice = products.First().Price
                        }
                    };
                }
                _ctx.SaveChanges();
            }
        }