예제 #1
0
        public void CreateOrUpdate(EmployeeBindingModel model)
        {
            Employee employee;

            using (var context = new EmployeesDatabase())
            {
                if (model.Id.HasValue)
                {
                    employee = context.Employees.FirstOrDefault(rec => rec.Id == model.Id);
                    if (employee == null)
                    {
                        throw new Exception("Элемент не найден");
                    }
                }
                else
                {
                    employee = new Employee
                    {
                        Name          = model.Name,
                        Surname       = model.Surname,
                        Patronymic    = model.Patronymic,
                        VacationStart = model.VacationStart,
                        Position      = model.Position
                    };
                    context.Employees.Add(employee);
                    context.SaveChanges();
                    return;
                }
                employee.Name          = model.Name;
                employee.Surname       = model.Surname;
                employee.Patronymic    = model.Patronymic;
                employee.VacationStart = model.VacationStart;
                employee.Position      = model.Position;

                context.SaveChanges();
            }
        }
예제 #2
0
 public void Delete(EmployeeBindingModel model)
 {
     using (var context = new EmployeesDatabase())
     {
         var employee = context.Employees.FirstOrDefault(rec => rec.Id == model.Id);
         if (employee != null)
         {
             context.Employees.Remove(employee);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Элемент не найден");
         }
     }
 }