public void AddEmployee(Employee employee, bool keepId = false)
        {
            VerifyEmployee(employee);

            var existingObjects   = this.GetAllEmployees();
            var employeeWithEmail = existingObjects.Where(e => e.Email == employee.Email).FirstOrDefault();

            if (employeeWithEmail != null)
            {
                throw new ArgumentException(String.Format("Employee with email: {0} already exists", employee.Email));
            }

            Employee employeeWithId;

            if (keepId)
            {
                employeeWithId = new Employee(employee, employee.EmployeeId);
            }
            else
            {
                int maxId = 0;
                for (int i = 0; i < existingObjects.Count(); i++)
                {
                    if (existingObjects[i].EmployeeId > maxId)
                    {
                        maxId = existingObjects[i].EmployeeId;
                    }
                }
                employeeWithId = new Employee(employee, maxId + 1);
            }

            var employeeWithIdExistng = existingObjects.Where(e => e.EmployeeId == employeeWithId.EmployeeId).FirstOrDefault();

            if (employeeWithIdExistng != null)
            {
                throw new ArgumentException(String.Format("Employee with EmployeeId: {0} already exists", employeeWithId.EmployeeId));
            }

            dbContext.Employees.Add(employeeWithId);
            dbContext.SaveChanges();
            _log.Info(String.Format("Employee added to db: {0}", employeeWithId));
        }
Esempio n. 2
0
        /// <summary>
        /// Generate test data and save into database
        /// </summary>
        private static void GenerateTestData()
        {
            List <Employee> employees = new List <Employee>();

            employees.Add(new Employee {
                Name = "Jan Kowalski", Email = "*****@*****.**"
            });
            employees.Add(new Employee {
                Name = "Krzysztof Nowak", Email = "*****@*****.**"
            });
            employees.Add(new Employee {
                Name = "Ala Jeden", Email = "*****@*****.**"
            });
            employees.Add(new Employee {
                Name = "Ola Dwa", Email = "*****@*****.**"
            });

            List <EmployeeHours> employeeHours = new List <EmployeeHours>();

            employeeHours.Add(new EmployeeHours {
                Value = 15, TimePeriod = new TimeSpan(), Employee = employees[0]
            });
            employeeHours.Add(new EmployeeHours {
                Value = 10, TimePeriod = new TimeSpan(), Employee = employees[0]
            });
            employeeHours.Add(new EmployeeHours {
                Value = 12, TimePeriod = new TimeSpan(), Employee = employees[0]
            });
            employeeHours.Add(new EmployeeHours {
                Value = 2, TimePeriod = new TimeSpan(), Employee = employees[2]
            });

            Console.WriteLine("Updating database!");

            using (var db = new BranchOfficeDbContext())
            {
                db.Database.EnsureDeleted();
                db.Database.Migrate();

                db.Employees.AddRange(employees);
                var countEmployees = db.SaveChanges();
                Console.WriteLine("{0} Employee records saved to database", countEmployees);
                db.EmployeeHoursCollection.AddRange(employeeHours);
                var countEmployeeHours = db.SaveChanges();
                Console.WriteLine("{0} EmployeeHours records saved to database", countEmployeeHours);

                Console.WriteLine();
                Console.WriteLine("Finished database update");

                Console.WriteLine("All Employees in database:");
                foreach (var employee in db.Employees)
                {
                    Console.WriteLine(employee);
                }
                Console.WriteLine("All EmployeeHours in database:");
                foreach (var eh in db.EmployeeHoursCollection)
                {
                    Console.WriteLine(eh);
                }
                Console.WriteLine();
            }
        }