コード例 #1
0
        // Problem 15 - Delete Project by ID
        private static void DeleteProjectById(SoftUniContext context, int id)
        {
            var projectToRemove = context.Projects.First(p => p.ProjectID == id);

            foreach (var employee in projectToRemove.Employees)
            {
                employee.Projects.Remove(projectToRemove);
            }

            context.Projects.Remove(projectToRemove);

            context.SaveChanges();
        }
コード例 #2
0
        // Problem 12 - Increase Salaries
        private static void IncreaseSalaryOfEmployeesFromDepartment(SoftUniContext context, int percentage)
        {
            var employees = context.Employees
                            .Where(e => e.Department.Name == "Engineering" ||
                                   e.Department.Name == "Tool Design" ||
                                   e.Department.Name == "Marketing" ||
                                   e.Department.Name == "Information Services");

            foreach (var employee in employees)
            {
                employee.Salary = employee.Salary * (1 + percentage * 0.01m);
                Console.WriteLine($"{employee.FirstName} {employee.LastName} (${employee.Salary:F6})");
            }

            context.SaveChanges();
        }
コード例 #3
0
        // Problem 06 - Adding a New Address and Updating Employee
        private static void AddAddressToEmployeeWithLastName(SoftUniContext context, string text, int townId, string lastName)
        {
            var employee = context.Employees
                           .FirstOrDefault(e => e.LastName == lastName);

            if (employee == null)
            {
                return;
            }

            employee.Address = new Address()
            {
                AddressText = text,
                TownID      = townId
            };

            context.SaveChanges();
        }
コード例 #4
0
        // Problem 16 - Remove Towns
        private static void RemoveTown(SoftUniContext context)
        {
            Console.Write("Town: ");
            string town = Console.ReadLine();

            var townToRemove = context.Towns.FirstOrDefault(t => t.Name == town);

            if (townToRemove == null)
            {
                return;
            }

            int count     = townToRemove.Addresses.Count;
            var addresses = townToRemove.Addresses.ToList();

            foreach (var address in addresses)
            {
                var employees = context.Employees.Where(e => e.AddressID == address.AddressID);
                foreach (var employee in employees)
                {
                    employee.AddressID = null;
                    employee.Address   = null;
                }

                context.Addresses.Remove(address);
            }

            context.Towns.Remove(townToRemove);

            context.SaveChanges();

            string addressStr = count == 1 ? "address" : "addresses";
            string isStr      = count == 1 ? "was" : "were";

            Console.WriteLine($"{count} {addressStr} in {town} {isStr} deleted");
        }