public void DeleteDepartment(int departmentId) { using (Training2020DbContext context = new Training2020DbContext()) { var department = context.Departments.Where(d => d.DepartmentId == departmentId).FirstOrDefault(); if (department != null) { context.Departments.Remove(department); context.SaveChanges(); } } }
public void AddDepartment(string departmentName) { using (Training2020DbContext context = new Training2020DbContext()) { Department department = new Department() { Name = departmentName }; context.Departments.Add(department); context.SaveChanges(); } }
public void UpdateDepartment(int departmentId, string newDepartmentName) { using (Training2020DbContext context = new Training2020DbContext()) { //var department = context.Departments.Find(departmentId); var department = context.Departments.Where(d => d.DepartmentId == departmentId).FirstOrDefault(); if (department != null) { department.Name = newDepartmentName; context.Entry(department).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); } } }