void RefreshList() { using (PeopleCarsContext ctx = new PeopleCarsContext()) { lvPeople.ItemsSource = ctx.People.ToList(); } }
private void miDelete_Click(object sender, RoutedEventArgs e) { Person currPerson = lvPeople.SelectedItem as Person; if (currPerson == null) { return; } // MessageBoxResult result = MessageBox.Show(this, "Are you sure you want to delete:\n" + currPerson, "Confirm delete", MessageBoxButton.OKCancel, MessageBoxImage.Warning); // using (PeopleCarsContext ctx = new PeopleCarsContext()) { if (result == MessageBoxResult.OK) { var people = (from p in ctx.People where p.Id == currPerson.Id select p).ToList(); if (people.Count == 0) { Console.WriteLine("Record not found"); } else { Person p = people[0]; ctx.People.Remove(p); ctx.SaveChanges(); RefreshList(); } } } }
private void btnSavePerson_Click(object sender, RoutedEventArgs e) { using (PeopleCarsContext ctx = new PeopleCarsContext()) { string name = tbName.Text; int age = int.Parse(tbAge.Text); string genderStr = cbGender.Text; Person.GenderEnum gender; if (!Enum.TryParse <Person.GenderEnum>(genderStr, out gender)) { throw new InvalidCastException("Enum value invalid: " + genderStr); } if (currPerson == null) { Person p = new Person { Name = name, Age = age, Gender = gender }; ctx.People.Add(p); ctx.SaveChanges(); } else { var people = (from p in ctx.People where p.Id == currPerson.Id select p).ToList(); if (people.Count == 0) { Console.WriteLine("Record not found"); } else { Person p = people[0]; p.Name = name; p.Age = age; p.Gender = gender; ctx.SaveChanges(); } } this.DialogResult = true; } }