// Method that delete Employee from database
 public void DeleteEmployee(int employeeID)
 {
     try
     {
         using (EmployeeDBEntities context = new EmployeeDBEntities())
         {
             //creating a list of employees for which employee with forwarded id is the manager
             var employeeOfThisManager = context.tblEmployees.Where(x => x.Manager == employeeID).ToList();
             //if the list is not empty, setting manager id to null for every employee in that list
             if (employeeOfThisManager.Count() > 0)
             {
                 foreach (var employee in employeeOfThisManager)
                 {
                     employee.Manager = null;
                     LogAction("Employee with ID " + employee.EmployeeID + " updated so he has no manager.");
                 }
             }
             //finding employee with forwarded id
             tblEmployee employeeToDelete = context.tblEmployees.Where(x => x.EmployeeID == employeeID).FirstOrDefault();
             //if that employee was the only in the sector, deleting sector
             var peopleInSector = context.tblEmployees.Where(x => x.Sector == employeeToDelete.Sector).ToList();
             if (peopleInSector.Count() == 1)
             {
                 var sector = context.tblSectors.Where(x => x.SectorID == employeeToDelete.Sector).FirstOrDefault();
                 context.tblSectors.Remove(sector);
                 context.SaveChanges();
                 LogAction("Sector " + sector.SectorName + " with ID: " + sector.SectorID + " deleted.");
             }
             //removing employee from DbSet and saving changes to database
             context.tblEmployees.Remove(employeeToDelete);
             context.SaveChanges();
             LogAction("Employee with ID " + employeeToDelete.EmployeeID + " deleted.");
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
예제 #2
0
 // This method checks if sector already exists in database
 public bool IsSectorExists(string sectorName)
 {
     try
     {
         using (EmployeeDBEntities context = new EmployeeDBEntities())
         {
             tblSector sector = context.tblSectors.Where(x => x.SectorName == sectorName).FirstOrDefault();
             if (sector != null)
             {
                 return(true);
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
         return(false);
     }
 }