Exemple #1
0
        /// <summary>
        /// This method removes a single employee from the database
        /// </summary>
        /// <param name="id">Integer value ID of the employee</param>
        /// <returns>0 = Failed</returns>
        public long Delete(long id)
        {
            try
            {
                using (var entity = new EmployeesEntities())
                {
                    var query = from item in entity.Employees
                                where (item.Id.Equals(id))
                                select item;

                    var employee = query.First();

                    entity.Employees.Remove(employee);
                    id = entity.SaveChanges();
                    return(id);
                }
            }
            catch { return(0); }
        }
Exemple #2
0
        /// <summary>
        /// Save employee object to the database using EF
        /// </summary>
        /// <param name="employee">Employee</param>
        /// <returns>0 = Failed</returns>
        public long Save(Employee employee)
        {
            try
            {
                var res = Exist(employee.Id);
                using (var entity = new EmployeesEntities())
                {
                    if (!res)
                    {
                        var id = entity.CreateEmployee(DateTime.Now, employee.Firstname, employee.Lastname, employee.DOB, employee.Gender, employee.Department, employee.Active).First().Value;
                        return(id);
                    }
                    else
                    {
                        var query = from item in entity.Employees
                                    where (item.Id.Equals(employee.Id))
                                    select item;

                        var current = query.FirstOrDefault <Employee>();
                        if (current != null)
                        {
                            current.Active     = employee.Active;
                            current.Department = employee.Department;
                            current.DOB        = employee.DOB;
                            current.Firstname  = employee.Firstname;
                            current.Gender     = employee.Gender;
                            current.Lastname   = employee.Lastname;

                            var id = entity.SaveChanges();
                            return(id);
                        }

                        return(0);
                    }
                }
            }
            catch { return(0); }
        }