private void buttonDeleteAnimal_Click(object sender, EventArgs e) { if (dataGridViewAnimals.CurrentRow == null) { return; } var selectedOwner = (Animal)dataGridViewAnimals.CurrentRow.DataBoundItem; using (RepositoryContext db = new RepositoryContext("RepositoryContext")) { AnimalRepository animalRepo = new AnimalRepository(db); var animal = animalRepo.GetById(selectedOwner.Id); animalRepo.Delete(animal); animalRepo.SaveChanges(); } GetData(); }
private void buttonAddNewAnimal_Click(object sender, EventArgs e) { if (comboBoxOwnerList.SelectedItem == null) { return; } int ownerId = ((Owner)comboBoxOwnerList.SelectedItem).Id; Animal newAnimal = new Animal { Name = textBoxAnimalName.Text, Breed = textBoxAnimalBreed.Text, DateOfBirth = dateTimePickerAnimalDate.Value, OwnerId = ownerId }; textBoxAnimalName.Text = ""; textBoxAnimalBreed.Text = ""; dateTimePickerAnimalDate.Text = ""; using (RepositoryContext db = new RepositoryContext("RepositoryContext")) { OwnerRepository ownerRepo = new OwnerRepository(db); AnimalRepository animalRepo = new AnimalRepository(db); var owner = ownerRepo.GetById(ownerId); newAnimal.Owner = owner; var animal = animalRepo.Add(newAnimal); animalRepo.SaveChanges(); } GetData(); }