private void AddPersonInDB(object o) { String PersonType = o as String; EditPersonWindow edw = new EditPersonWindow(PersonType); if (edw.ShowDialog() == true) { Person p = edw.GetPerson(); if (p is null) { MessageBox.Show("Can't add a person. Incorrect data"); return; } string type = edw.GetPersonType(); switch (type) { case "Employee": db.Employees.Add((Employee)p); db.SaveChanges(); People = new ObservableCollection <Person>(db.People.Local.ToList()); Employees = new ObservableCollection <Employee>(db.Employees.Local.ToList()); break; case "Customer": db.Customers.Add((Customer)p); db.SaveChanges(); People = new ObservableCollection <Person>(db.People.ToList()); Customers = new ObservableCollection <Customer>(db.Customers.Local.ToList()); break; case "Seller": db.Sellers.Add((Seller)p); db.SaveChanges(); People = new ObservableCollection <Person>(db.People.ToList()); Sellers = new ObservableCollection <Seller>(db.Sellers.Local.ToList()); break; default: break; } #if DEBUG string s = String.Format("Debug message: Added person, Type of person - {0}, Name - {1}, Phone - {2}", type, p?.Name, p?.Phone); MessageBox.Show(s); #endif } }
private void EditPersonInDB(object obj) { Person p = null; if (obj == null) { MessageBox.Show("Person is not selected"); return; } if (obj is Employee) { Employee emp = obj as Employee; p = new Employee() { Id = emp.Id, Name = emp.Name, Phone = emp.Phone, Post = emp.Post }; } else if (obj is Customer) { Customer cust = obj as Customer; p = new Customer() { Id = cust.Id, Name = cust.Name, Phone = cust.Phone, Level = cust.Level }; } else if (obj is Seller) { Seller sel = obj as Seller; p = new Seller() { Id = sel.Id, Name = sel.Name, Phone = sel.Phone, Reliability = sel.Reliability }; } EditPersonWindow edw = new EditPersonWindow(p); if (edw.ShowDialog() == true) { p = edw.GetPerson(); if (p is null) { MessageBox.Show("Can't edit a person. Incorrect data"); return; } string type = edw.GetPersonType(); switch (type) { case "Employee": Employee emp = db.Employees.Find(p.Id); if (emp != null) { emp.Name = p.Name; emp.Phone = p.Phone; emp.Post = ((Employee)p).Post; //db.Entry(emp).State = EntityState.Modified; db.SaveChanges(); } break; case "Customer": Customer cust = db.Customers.Find(p.Id); if (cust != null) { cust.Name = p.Name; cust.Phone = p.Phone; cust.Level = ((Customer)p).Level; //db.Entry(cust).State = EntityState.Modified; db.SaveChanges(); } break; case "Seller": Seller sel = db.Sellers.Find(p.Id); if (sel != null) { sel.Name = p.Name; sel.Phone = p.Phone; sel.Reliability = ((Seller)p).Reliability; //db.Entry(sel).State = EntityState.Modified; db.SaveChanges(); } break; default: break; } #if DEBUG string s = String.Format("Debug message: Edited person, Type of person - {0}, Name - {1}, Phone - {2}", type, p?.Name, p?.Phone); MessageBox.Show(s); #endif } }