public IActionResult Create(Pet pet) { _storeContext.Pets.Add(pet); _storeContext.SaveChanges(); return(CreatedAtRoute("GetPet", new { id = pet.Id }, pet)); }
public IActionResult Create(Owner owner) { _storeContext.Owners.Add(owner); _storeContext.SaveChanges(); return(CreatedAtRoute("GetOwner", new { id = owner.Id }, owner)); }
public ActionResult Create([Bind(Include = "PetID,PetName,PetDescription,create_dt,AnimalTypeCD")] Pet pet) { if (ModelState.IsValid) { db.Pets.Add(pet); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.AnimalTypeCD = new SelectList(db.AnimalTypes, "AnimalTypeCD", "AnimalTypeName", pet.AnimalTypeCD); return(View(pet)); }
public int Add(Pet petEntity) { int result = -1; if (petEntity != null) { _context.Pets.Add(petEntity); _context.SaveChanges(); result = petEntity.Id; } return(result); }
public void DeleteAnimal(long id) { Animal toDelete = GetAnimal(id); if (toDelete != null) { _context.Animals.Remove(toDelete); _context.SaveChanges(); } else { throw new Exception($"Could not find an animal with the ID '{id}'"); } }
public void AddPet(Pet pet) { using (var db = new PetStoreContext()) { db.Pets.Add(pet); db.SaveChanges(); } }
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 void SavePet(Pet pet) { Breed breed = _petContext.Breeds.Find(pet.Breed.Id); pet.Breed = breed; _petContext.Pets.Add(pet); _petContext.SaveChanges(); }
public IActionResult Put(int id) { using (var db = new PetStoreContext()) { db.Pets.Add(new Pet(2, "Amos", "Cat")); db.SaveChanges(); } return(Ok()); }
protected override void Up(MigrationBuilder migrationBuilder) { using (var db = new PetStoreContext()) { db.Pets.Add(new Pet(1, "Zoey", "Cat")); db.Pets.Add(new Pet(2, "Norman", "Dog")); db.SaveChanges(); } }
public PetsController(PetStoreContext storeContext) { _storeContext = storeContext; if (_storeContext.Pets.Count() == 0) { _storeContext.Pets.Add(new Pet { Name = "Cat", Family = "Cat" }); _storeContext.SaveChanges(); } }
public OwnersController(PetStoreContext context) { _storeContext = context; if (_storeContext.Owners.Count() == 0) { _storeContext.Owners.Add(new Owner { Name = "Dileep" }); _storeContext.SaveChanges(); } }
public bool DeletePet(int petId) { using (var db = new PetStoreContext()) { var toRemove = db.Pets.FirstOrDefault(p => p.Id == petId); if (toRemove != null) { db.Pets.Remove(toRemove); db.SaveChanges(); return(true); } return(false); } }
public void SaveBreed(Breed breed) { if (!BreedExists(breed.Animal.Id, breed.Name)) { Animal matchingAnimal = _context.Animals.Find(breed.Animal.Id); breed.Animal = matchingAnimal; _context.Breeds.Add(breed); _context.SaveChanges(); } else { throw new Exception("Breed already exists"); } }
public IActionResult CriarPet([FromBody] PetRequestDTO petDto) { var categoria = petRepository.ObterCategoriaPorId(petDto.IdCategoria); if (categoria == null) { return(BadRequest("A categoria é obrigatoria")); } var pet = new Pet(petDto.Nome, petDto.Status, categoria, petDto.Tags.Select(tag => new Tag(tag)).ToList()); var petCadastrado = petRepository.Cadastrar(pet); contexto.SaveChanges(); return(CreatedAtRoute("GetPet", new { id = petCadastrado.Id }, petCadastrado)); }
static void EditExistingRecord() { //To edit a record, we first need to get a reference to it; this requires querying the table. using PetStoreContext context = new PetStoreContext(); { var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); //check that a non null object was returned. if (squeakyBone is Product) { //Modify the object's data. squeakyBone.Price = 7.99m; } //Commit the changes. context.SaveChanges(); } }
static void DeleteExistingRecord() { //To edit a record, we first need to get a reference to it; this requires querying the table. using PetStoreContext context = new PetStoreContext(); { var squeakyBone = context.Products.Where(p => p.Name == "Squeaky Dog Bone") .FirstOrDefault(); //check that a non null object was returned. if (squeakyBone is Product) { //Remove the object context.Remove(squeakyBone); Console.WriteLine("removed object."); } //Commit the changes. context.SaveChanges(); } }
public ActionResult Create(Pet pet) { if (!ModelState.IsValid) { return(View(pet)); } try { PetStoreContext context = new PetStoreContext(); context.Pets.Add(pet); context.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("Error", ex.Message); return(View(pet)); } TempData["Message"] = "Created " + pet.Name; return(RedirectToAction("Index")); }
public int Complete() { return(_context.SaveChanges()); }