private static void InsertNewEmployee(SoftUniEntities db, string firstName, string lastName, string middleName, string jobTitle, string department, string manager, DateTime dateTime, double salary, string address) { var idDepartment = db.Departments.FirstOrDefault(d => d.Name == department).DepartmentID; var idManager = db.Employees.FirstOrDefault(e => e.FirstName + " " + e.LastName == manager).EmployeeID; var idAddress = db.Addresses.FirstOrDefault(a => a.AddressText == address).AddressID; var newEmployee = new Employee { FirstName = firstName, LastName = lastName, MiddleName = middleName, AddressID = idAddress, ManagerID = idManager, DepartmentID = idDepartment, JobTitle = jobTitle, HireDate = dateTime, Salary = (decimal)salary }; db.Employees.Add(newEmployee); db.SaveChanges(); }
private static void ModifyEmployee(SoftUniEntities db, int id, string firstName, string lastName, string middleName, string jobTitle, string department, string manager, DateTime dateTime, double salary, string address) { var employee = db.Employees.FirstOrDefault(e => e.EmployeeID == id); if (employee == null) { throw new NullReferenceException(); } employee.DepartmentID = db.Departments.FirstOrDefault(d => d.Name == department).DepartmentID; employee.ManagerID = db.Employees.FirstOrDefault(e => e.FirstName + " " + e.LastName == manager).EmployeeID; employee.AddressID = db.Addresses.FirstOrDefault(a => a.AddressText == address).AddressID; employee.FirstName = firstName; employee.LastName = lastName; employee.MiddleName = middleName; employee.Salary = (decimal)salary; employee.HireDate = dateTime; employee.JobTitle = jobTitle; db.Entry(db.Employees.FirstOrDefault(e => e.EmployeeID == id)).CurrentValues.SetValues(employee); db.SaveChanges(); }
private static void DeleteEmployee(SoftUniEntities db, int id) { var employee = db.Employees.FirstOrDefault(e => e.EmployeeID == id); db.Employees.Remove(employee); db.SaveChanges(); }