Beispiel #1
0
        /// <summary>
        /// Edit or Add new employee
        /// </summary>
        /// <param name="employee">added or edited employee</param>
        /// <returns></returns>
        public static vwEmployee AddNewEmployee(vwEmployee employee)
        {
            try
            {
                using (EmployeeEntities context = new EmployeeEntities())
                {
                    //update
                    if (employee.employeeId != 0)
                    {
                        tblEmployee employeeToEdit = (from c in context.tblEmployees where c.employeeId == employee.employeeId select c).First();
                        employeeToEdit.fullname           = employee.fullname;
                        employeeToEdit.dateOfBirth        = employee.dateOfBirth;
                        employeeToEdit.genderId           = employee.getTblGender.genderId;
                        employeeToEdit.IdentityCardNumber = employee.IdentityCardNumber;
                        employeeToEdit.jmbg       = employee.jmbg;
                        employeeToEdit.phone      = employee.phone;
                        employeeToEdit.locationId = employee.getTblLocation.locationId;
                        if (employee.getTblManager != null)
                        {
                            employeeToEdit.managerId = employee.getTblManager.employeeId;
                        }
                        else
                        {
                            employeeToEdit.managerId = null;
                        }
                        //check existance of sector name. Add new one if sectorName do not exist
                        tblSector s = AddNewSector(employee.sectorName);
                        employeeToEdit.sectorId = s.sectorId;
                        context.SaveChanges();
                        return(employee);
                    }
                    //add new
                    else
                    {
                        tblEmployee newEmployee = new tblEmployee();
                        newEmployee.fullname    = employee.fullname;
                        newEmployee.dateOfBirth = employee.dateOfBirth;
                        newEmployee.genderId    = employee.getTblGender.genderId;

                        newEmployee.IdentityCardNumber = employee.IdentityCardNumber;
                        newEmployee.jmbg       = employee.jmbg;
                        newEmployee.phone      = employee.phone;
                        newEmployee.locationId = employee.getTblLocation.locationId;
                        if (employee.getTblManager != null)
                        {
                            newEmployee.managerId = employee.getTblManager.employeeId;
                        }
                        else
                        {
                            newEmployee.managerId = null;
                        }
                        //check existance of sector name. Add new one if sectorName do not exist
                        tblSector s = AddNewSector(employee.sectorName);
                        newEmployee.sectorId = s.sectorId;
                        context.tblEmployees.Add(newEmployee);
                        context.SaveChanges();
                        employee.employeeId = newEmployee.employeeId;
                        return(employee);
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }