示例#1
0
        public Category Create(string Name, string Description)
        {
            Category category = new Category()
            {
                Name = Name, Description = Description
            };

            _commerceDbContext.Category.Add(category);
            _commerceDbContext.SaveChanges();
            return(category);
        }
示例#2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            using (var context = new CommerceDbContext(
                       serviceProvider.GetRequiredService <
                           DbContextOptions <CommerceDbContext> >()))
            {
                // Look for any thing.
                if (context.Users.Any())
                {
                    return;   // DB has been seeded
                }
                if (context.Products.Any())
                {
                    return;   // DB has been seeded
                }

                context.Users.AddRange(
                    new User
                {
                    Name     = "Truong Tien",
                    Username = "******",
                    Password = "******"
                }
                    );
                context.SaveChanges();

                context.Products.AddRange(
                    new Product
                {
                    Title       = "Iphone 11 pro",
                    Image       = "https://res.cloudinary.com/summonersrift/image/upload/v1616489223/Picture/iPhone_11_Pro_Max_256GB_d19ax9.jpg",
                    Price       = 999,
                    Sale        = 799,
                    Quantity    = 1000,
                    Description = "abc xyz 123"
                }
                    );
                context.SaveChanges();

                context.Pictures.AddRange(
                    new Picture
                {
                    IdProduct = 1,
                    Image     = "https://res.cloudinary.com/summonersrift/image/upload/v1616489721/Picture/iPhone_11_Pro_Max_64GB_yrrmiz.jpg",
                },
                    new Picture
                {
                    IdProduct = 1,
                    Image     = "https://res.cloudinary.com/summonersrift/image/upload/v1616489733/Picture/iPhone_11_Pro_Max_512GB_u9udnt.jpg",
                }
                    );
                context.SaveChanges();
            }
        }
示例#3
0
        public ActionResult Create([Bind(Include = "ProductID,Name,Price")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(product));
        }
示例#4
0
 public Product Create(Product product)
 {
     product.CreateDate = DateTime.Now;
     if (product.Id == 0)
     {
         _commerceDbContext.Product.Add(product);
         _commerceDbContext.SaveChanges();
     }
     else
     {
         _commerceDbContext.Product.Update(product);
         _commerceDbContext.SaveChanges();
     }
     return(product);
 }
        public AppUser Edit(int id, AppUser appUser)
        {
            var user = _userManager.Users.FirstOrDefault(x => x.Id == id);

            user.UserName             = appUser.UserName;
            user.AccessFailedCount    = appUser.AccessFailedCount;
            user.EmailConfirmed       = appUser.EmailConfirmed;
            user.Email                = appUser.Email;
            user.LockoutEnabled       = appUser.LockoutEnabled;
            user.LockoutEnd           = appUser.LockoutEnd;
            user.NormalizedEmail      = appUser.NormalizedEmail;
            user.NormalizedUserName   = appUser.NormalizedUserName;
            user.PasswordHash         = appUser.PasswordHash;
            user.PhoneNumber          = appUser.PhoneNumber;
            user.PhoneNumberConfirmed = appUser.PhoneNumberConfirmed;
            user.SecurityStamp        = appUser.SecurityStamp;
            user.ConcurrencyStamp     = appUser.ConcurrencyStamp;
            //user.TwoFactorEnabled = appUser.TwoFactorEnabled;
            _userManager.UpdateAsync(user).GetAwaiter().GetResult();
            _context.SaveChanges();
            return(user);
        }
示例#6
0
        public static void Seed(IServiceProvider serviceProvider)
        {
            using (var context = new CommerceDbContext(serviceProvider.GetRequiredService <DbContextOptions <CommerceDbContext> >()))
            {
                if (context.Contractors.Count() < 10)
                {
                    for (int i = 0; i < 10000; i++)
                    {
                        AddContractors(context);
                    }

                    context.SaveChanges();
                }
            }
        }
        public IActionResult Create(Contractor contractor)
        {
            if (ModelState.IsValid)
            {
                dbContext.Add <Contractor>(contractor);
                dbContext.SaveChanges();
                ViewData["Success"] = "Contractor created successfully";
            }
            else
            {
                ViewData["Error"] = "An error occur during creating new contractor";

                string[] errorMessages = ModelState.Values.SelectMany(v => v.Errors).Select(v => v.ErrorMessage).ToArray();

                ViewData["ErrorMessages"] = errorMessages;
            }

            return(View());
        }
        private static void UseEnsureCreated(CommerceDbContext db)
        {
            if (db.Database.EnsureCreated())
            {
                // add some data

                db.Products.Add(new Product
                {
                    Description = "il TV",
                    Price       = 1234.56m
                });
                db.Products.Add(new Product
                {
                    Description = "Dash Button",
                    Price       = 4.99m
                });

                db.SaveChanges();
            }
        }
        private static void UseMigration(CommerceDbContext db)
        {
            // Add-Migration InitialCreate

            db.Database.Migrate();

            // POSSO AGGIUNGERE I DATI COSI' OPPURE ATTRAVERSO UNA MIGRAZIONE

            var tv = db.Products.FirstOrDefault(x => x.Description == "il TV");

            if (tv == null)
            {
                db.Products.Add(new Product
                {
                    Description = "il TV",
                    Price       = 1234.56m
                });
            }
            else
            {
                tv.Price = 1234.56m;
            }

            var dashButton = db.Products.FirstOrDefault(x => x.Description == "Dash Button");

            if (tv == null)
            {
                db.Products.Add(new Product
                {
                    Description = "Dash Button",
                    Price       = 4.99m
                });
            }
            else
            {
                dashButton.Price = 4.99m;
            }

            db.SaveChanges();
        }
示例#10
0
 public ProductImage Create(ProductImage productImage)
 {
     _commerceDbContext.ProductImage.Add(productImage);
     _commerceDbContext.SaveChanges();
     return(productImage);
 }
示例#11
0
 public Order Create(Order order)
 {
     _commerceDbContext.Order.Add(order);
     _commerceDbContext.SaveChanges();
     return(order);
 }