public void AddEmployee(Employee employee) { // Only attempt to add the employee if there's is not already an employee // with the specified email in the database if (!_context.Employees.Any(e => e.Email == employee.Email)) { _context.Employees.Add(employee); _context.SaveChanges(); } }
public void AddDepartment(string deptName) { // Only attempt to add the dept if there is NOT one with the same name already if (!_context.Departments.Any(d => d.DepartmentName == deptName) && !String.IsNullOrWhiteSpace(deptName)) { _context.Departments.Add( new Department { DepartmentName = deptName } ); _context.SaveChanges(); } }
private static void EnsureDatabaseIsSeeded(EmployeeContactsContext context) { if (!context.Departments.Any()) { var hr = new Department { Id = 1, DepartmentName = "Human Resources" }; var finance = new Department { Id = 2, DepartmentName = "Finance" }; context.Departments.Add(hr); context.Departments.Add(finance); context.SaveChanges(); var employee1 = new Employee { Id = 1, DepartmentId = hr.Id, FirstName = "Mike", LastName = "Jones", Title = "HR Representitive", Email = "*****@*****.**", Phone = "555-123-4567" }; var employee2 = new Employee { Id = 2, DepartmentId = hr.Id, FirstName = "Jane", LastName = "Smith", Title = "HR Manager", Email = "*****@*****.**", Phone = "555-123-1111" }; var employee3 = new Employee { Id = 3, DepartmentId = finance.Id, FirstName = "Hector", LastName = "Flores", Title = "CPA", Email = "*****@*****.**", Phone = "555-123-2222" }; var employee4 = new Employee { Id = 4, DepartmentId = finance.Id, FirstName = "Emily", LastName = "Radnor", Title = "CPA", Email = "*****@*****.**", Phone = "555-123-4444" }; var employee5 = new Employee { Id = 5, DepartmentId = finance.Id, FirstName = "Sarah", LastName = "Jackson", Title = "CPA Intern", Email = "*****@*****.**", Phone = "555-123-5555" }; context.Employees.Add(employee1); context.Employees.Add(employee2); context.Employees.Add(employee3); context.Employees.Add(employee4); context.Employees.Add(employee5); context.SaveChanges(); } }