Esempio n. 1
0
 public static void InsertEmployee(Employee employee)
 {
     using (var db = new SoftUniEntities())
     {
         db.Employees.Add(employee);
         db.SaveChanges();
     }
 }
Esempio n. 2
0
 public void Initialize()
 {
     this.db = new SoftUniEntities();
     this.employee = new Employee
     {
         EmployeeID = int.MaxValue,
         FirstName = "Test",
         MiddleName = "User",
         LastName = "Name",
         DepartmentID = 1,
         JobTitle = "CCleaner",
         HireDate = new DateTime(2000, 03, 07),
         Salary = 2000.50m
     };
 }
        public static void AddNewEmployee(
            string firstName,
            string lastName,
            string jobTitle,
            string department,
            DateTime hireDate,
            decimal salary,
            string middleName = null,
            string manager = null,
            string address = null,
            string town = null)
        {
            var db = new SoftUniEntities1();
            int newEmployeeDepartmentId = db.Departments
                .Where(d => d.Name == department)
                .Select(d => d.DepartmentID)
                .FirstOrDefault();

            int? employeeManagerId = null;
            int? employeeAddressId = null;
            int? employeeTownId = null;

            if (!string.IsNullOrEmpty(manager))
            {
                employeeManagerId = db.Employees
                    .Where(e => e.FirstName + " " + e.LastName == manager)
                    .Select(e => e.EmployeeID)
                    .FirstOrDefault();
            }

            if (!string.IsNullOrEmpty(address))
            {
                if (!string.IsNullOrEmpty(town))
                {
                    employeeTownId = db.Towns
                        .Where(t => t.Name == town)
                        .Select(t => t.TownID)
                        .FirstOrDefault();
                }

                var employeeAddress = new Address
                {
                    AddressText = address,
                    TownID = employeeTownId
                };

                db.Addresses.Add(employeeAddress);
                db.SaveChanges();

                employeeAddressId = db.Addresses
                    .Max(a => a.AddressID);
            }

            var newEmployee = new Employee
            {
                FirstName = firstName,
                LastName = lastName,
                MiddleName = middleName,
                JobTitle = jobTitle,
                DepartmentID = newEmployeeDepartmentId,
                ManagerID = employeeManagerId,
                HireDate = hireDate,
                Salary = salary,
                AddressID = employeeAddressId
            };

            db.Employees.Add(newEmployee);
            db.SaveChanges();
            Console.WriteLine("Employee added");
        }
Esempio n. 4
0
        public static void ModifyEmployee(
            Employee employee,
            string FirstName = null,
            string MiddleName = null,
            string LastName = null,
            string JobTitle = null,
            int? DepartmentID = null,
            int? ManagerID = null,
            DateTime? HireDate = null,
            decimal? Salary = null,
            int? AddressID = null
            )
        {
            using (var db = new SoftUniEntities())
            {
                var emp = db.Employees.Find(employee.EmployeeID);

                if (FirstName != null)
                {
                    emp.FirstName = FirstName;
                }
                if (MiddleName != null)
                {
                    emp.MiddleName = MiddleName;
                }
                if (LastName != null)
                {
                    emp.LastName = LastName;
                }
                if (JobTitle != null)
                {
                    emp.JobTitle = JobTitle;
                }
                if (DepartmentID != null)
                {
                    emp.DepartmentID = (int)DepartmentID;
                }
                if (ManagerID != null)
                {
                    emp.ManagerID = ManagerID;
                }
                if (HireDate != null)
                {
                    emp.HireDate = (DateTime)HireDate;
                }
                if (Salary != null)
                {
                    emp.Salary = (decimal)Salary;
                }
                if (AddressID != null)
                {
                    emp.AddressID = AddressID;
                }

                db.SaveChanges();
            }
        }