static void AddProductsToDatabase() { using PetStoreContext context = new PetStoreContext(); { Product squeakyBone = new Product() { Name = "Squeaky Dog Bone", Price = 4.99M }; //We have the option of explicitly adding the object to its associated table. This is not necessary (see below) context.Products.Add(squeakyBone); Product tennisBalls = new Product() { Name = "Tennis Ball 3-Pack", Price = 9.99M }; //Here, we add the object only to the context. Entity will recognize the object and assign it to the correct table. context.Add(tennisBalls); //Commit the changes to the context. context.SaveChanges(); } }
public async Task <IActionResult> Create([Bind("ID,FirstName,LastName,Street,City,EmailAddress,PhoneNumber,Occupation")] Owner owner) { if (ModelState.IsValid) { _context.Add(owner); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(owner)); }
public async Task <IActionResult> Create([Bind("ID,Name,DOB,OwnderId")] Pet pet) { if (ModelState.IsValid) { _context.Add(pet); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(pet)); }
public async Task <IActionResult> Create([Bind("PetID,Name,DOB,Breed,OwnerID")] Pet pet) { if (ModelState.IsValid) { _context.Add(pet); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["OwnerID"] = new SelectList(_context.Owner, "ID", "FullName", pet.OwnerID); return(View(pet)); }