public static void InsertEmployees(List <Employee> employees, PayrollContext context) { var departments = context.Departments.ToList(); foreach (var employee in employees) { int id = departments.Where(d => d.Name == employee.Department).First().Id; employee.DepartmentId = id; } context.AddRange(employees); context.SaveChanges(); }