public static void Run() { var _db = new MyContext(); var deparment = new Deparment { DeparmentName = "Deparment" }; var employee1 = new Employee { EmployeeName = "employee1" }; var employee2 = new Employee { EmployeeName = "employee2" }; var employeeDeparment1 = new EmployeeDeparment { Employee = employee1, Deparment = deparment }; var employeeDeparment2 = new EmployeeDeparment { Employee = employee2, Deparment = deparment }; _db.Add(employeeDeparment1); _db.Add(employeeDeparment2); _db.SaveChanges(); var employees = _db.Employee.ToList(); foreach (var employee in employees) { Console.WriteLine(employee.EmployeeName); } Console.ReadLine(); }
private void btn_addEmployee_Click(object sender, EventArgs e) { // build employee section Employee emp = new Employee { FirstName = tb_firstName.Text, MiddleName = tb_middleName.Text, LastName = tb_lastName.Text, SSN = tb_ssn.Text }; // build address section Address addr = new Address { StreetNumber = tb_streetNumber.Text, City = tb_city.Text, State = cb_state.SelectedValue.ToString(), ZipCode = tb_zipCode.Text }; // build salary section Salary sal = new Salary(); decimal money = 0; decimal.TryParse(tb_salary.Text, out money); sal.Salary1 = money; // build department section // if nothing is set, then assume employee is assigned to "Not Assigned" which has an id of 1 int id = 1; foreach (Department d in departments.Where(d => cb_departmentName.SelectedIndex.ToString() == d.DepartmenName)) { id = d.DepartmentId; } EmployeeDeparment empDep = new EmployeeDeparment { DepartmentId = id }; id = 0; // build supervisor section // note: not everyone has to have a supervisor... foreach (Employee employee in from employee in supervisors let name = employee.MiddleName == string.Empty ? employee.FirstName + " " + employee.LastName : employee.FirstName + " " + employee.MiddleName + " " + employee.LastName where name == cb_supervisor.SelectedIndex.ToString() select employee) { id = employee.EmployeeId; } Supervisor super = new Supervisor { SupervisorEmployeeId = id }; // build dates employeed // note: ending date can be empty...since they can be currently employed EmployeeService empService = new EmployeeService { StartDate = datePicker_startDate.Value, EndDate = datePicker_endDate.Value }; // create new employee model EmployeeModel newEmployee = new EmployeeModel { Employee = emp, Address = addr, Salary = sal, EmployeeDeparment = empDep, Supervisor = super, EmployeeService = empService }; // finally build the complete model to pass down // call middleware to do logic prior to adding employee to DB string result = businessLogic.AddEmployee(newEmployee); if (result == "Employee added") { ClearControls(); UpdateComboBoxes(); } MessageBox.Show(result); }