// ADD public static void Add(Employee employee) { using (var db = new SoftUniEntities()) { db.Employees.Add(employee); db.SaveChanges(); Console.WriteLine("Employee added successiful."); } }
// DELETE public static void Delete(int id) { using (var db = new SoftUniEntities()) { var employeeToDelete = db.Employees.Find(id); if (employeeToDelete != null) { db.Employees.Remove(employeeToDelete); db.SaveChanges(); Console.WriteLine("Employee with ID: {0} removed.", id); } else { Console.WriteLine("Employee with ID {0} was not found.", id); } } }
// MODIFY public static void Modify(int id, string propertyToUpdate, string updateValue) { using (var db = new SoftUniEntities()) { var employee = db.Employees.Find(id); try { employee.GetType().GetProperty(propertyToUpdate).SetValue(employee, updateValue); db.SaveChanges(); Console.WriteLine("Employee with ID: {0} -> {1} has changed to: {2}", id, propertyToUpdate, updateValue); } catch (NullReferenceException) { Console.Error.WriteLine("Unknown property/id parameter."); } } }