private void RemoveTown(string townName, SoftuniContext context) { var deletingTown = context.Towns .FirstOrDefault(t => t.Name.Equals(townName, StringComparison.OrdinalIgnoreCase)); if (deletingTown == null) { Console.WriteLine($"Town {townName} not found."); return; } foreach (var employee in context.Employees.Where(e => e.Address.TownID == deletingTown.TownID)) { employee.Address = null; } var deletingAddresses = context.Addresses .Where(a => a.TownID == deletingTown.TownID).ToArray(); context.Addresses.RemoveRange(deletingAddresses); context.Towns.Remove(deletingTown); context.SaveChanges(); Console.WriteLine($"{deletingAddresses.Length} address in {deletingTown.Name} was deleted"); }
private void AssignANewAddress(string familyToAssignNewAddress, string newAddressText, int townId, SoftuniContext context) { var newAddress = new Address() { AddressText = newAddressText, TownID = townId }; foreach (var employee in context .Employees.Where(e => e.LastName == familyToAssignNewAddress)) { employee.Address = newAddress; } context.SaveChanges(); }
private void IncreaseSalariesByDepartment(int percentageIncreasement, SoftuniContext context, params string[] departmentNames) { var employees = context.Employees .Where(e => departmentNames.Contains(e.Department.Name)); foreach (var employee in employees) { employee.Salary *= 1.12M; } context.SaveChanges(); foreach (var employee in employees) { Console.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F6})"); } }
private void DeleteProject(int projectId, SoftuniContext context) { var project = context.Projects.Find(projectId); if (project == null) { return; } foreach (var employee in context.Employees.Where(e => e.Projects.Any(p => p.ProjectID == projectId))) { employee.Projects.Remove(project); } context.Projects.Remove(project); context.SaveChanges(); }