public async Task <IActionResult> Create(Product product) { try { if (ModelState.IsValid) { if (!ProductExists(product.Name)) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ViewBag.Message = ("Name already exists."); return(View(product)); } } return(View(product)); } catch (Exception e) { return(RedirectToAction(nameof(Error), new { message = e.Message })); } }
public void Seed() { if (_context.Product.Any() || _context.People.Any()) { return; } else { // popular banco de dados // Product Product pr1 = new Product { Name = "Computador", Value = 1000M }; Product pr2 = new Product { Name = "Celular", Value = 500M }; Product pr3 = new Product { Name = "Notebook", Value = 900M }; Product pr4 = new Product { Name = "Ipad", Value = 1500M }; // People People pl1 = new People { Name = "João", Idade = 30 }; People pl2 = new People { Name = "Maria", Idade = 33 }; _context.Product.AddRange(pr1, pr2, pr3, pr4); _context.People.AddRange(pl1, pl2); _context.SaveChangesAsync(); } }
public async Task InsertAsync(Offer obj) { _context.Add(obj); await _context.SaveChangesAsync(); }