コード例 #1
0
        public static void Main()
        {
            var context           = new SoftUniContext();
            var employeesProjects = context.EmployeesProjects.Where(i => i.Project.ProjectId == 2);
            var projects          = context.Projects.Where(id => id.ProjectId == 2);

            context.RemoveRange(employeesProjects);
            context.RemoveRange(projects);
            context.SaveChanges();
            var result = context.Projects.Select(x => x.Name).Take(10);

            foreach (var project in result)
            {
                Console.WriteLine(project);
            }
        }
コード例 #2
0
        static void Main()
        {
            string townInput = Console.ReadLine();

            using (SoftUniContext context = new SoftUniContext())
            {
                var town = context.Towns.FirstOrDefault(t => t.Name == townInput);

                var addresses = context.Addresses.Where(a => a.TownId == town.TownId).ToArray();

                int deletedAddresses = addresses.Length;

                foreach (var a in addresses)
                {
                    var employees = context.Employees.Where(e => e.AddressId == a.AddressId);

                    foreach (var e in employees)
                    {
                        e.AddressId = null;
                    }
                }

                context.RemoveRange(addresses);
                context.Remove(town);
                context.SaveChanges();

                string address   = deletedAddresses == 1 ? "address" : "addresses";
                string wasOrWere = deletedAddresses == 1 ? "was" : "were";
                Console.WriteLine($"{deletedAddresses} {address} in {townInput} {wasOrWere} deleted");
            }
        }
コード例 #3
0
        public static string DeleteProjectById(SoftUniContext context)
        {
            StringBuilder output = new StringBuilder();

            var projectToDelete = context.Projects.First(p => p.ProjectId == 2);

            var employeeProjectsRecords = context.EmployeesProjects.Where(ep => ep.ProjectId == 2);

            context.RemoveRange(employeeProjectsRecords);

            context.SaveChanges();

            context.Remove(projectToDelete);

            context.SaveChanges();

            var projectsNames = context.Projects.Take(10).Select(p => p.Name).ToList();

            foreach (var projectName in projectsNames)
            {
                output.AppendLine(projectName);
            }

            return(output.ToString().TrimEnd());
        }
コード例 #4
0
        static void Main()
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                string         townName  = Console.ReadLine();
                Town           town      = context.Towns.FirstOrDefault(t => t.Name == townName);
                List <Address> addresses = context.Addresses
                                           .Include(a => a.Employees)
                                           .Where(a => a.TownId == town.TownId)
                                           .ToList();

                addresses.ForEach(a =>
                {
                    a.Employees.ToList().ForEach(e => e.AddressId = null);
                    a.Employees.Clear();
                });

                context.RemoveRange(addresses);
                context.Remove(town);
                context.SaveChanges();

                Console.WriteLine("{0} address in {1} was deleted", addresses.Count(), townName);
                //ss
            }
        }
コード例 #5
0
        public static string RemoveTown(SoftUniContext context)
        {
            string townName = "Seattle";

            var employees = context.Employees
                            .Where(e => e.Address.Town.Name == townName)
                            .ToList();

            //.ForEach(e => e.AddressId = null);

            foreach (var employee in employees)
            {
                employee.AddressId = null;
            }


            var addresses = context.Addresses
                            .Where(a => a.Town.Name == townName)
                            .ToList();

            //.ForEach(a => context.Addresses.Remove(a));

            // int addressesCount = addresses.Count();

            context.RemoveRange(addresses);

            context.Towns
            .Remove(context.Towns
                    .SingleOrDefault(t => t.Name == townName));

            context.SaveChanges();

            return($"{addresses.Count()} addresses in Seattle were deleted");
        }
コード例 #6
0
        static void Main()
        {
            using (SoftUniContext context = new SoftUniContext())
            {
                Project project = context.Projects.Find(2);
                IEnumerable <EmployeesProject> employeesProjects = context.EmployeesProjects.Where(ep => ep.ProjectId == 2);

                context.RemoveRange(employeesProjects);
                context.Remove(project);
                context.SaveChanges();

                context.Projects.Take(10).ToList().ForEach(p => Console.WriteLine(p.Name));
            }
        }
コード例 #7
0
        public static void Main(string[] args)
        {
            using (var db = new SoftUniContext())
            {
                var projectToBeDeleted = db.Projects.FirstOrDefault(p => p.ProjectId == 2);

                db.RemoveRange(db.EmployeesProjects
                               .Where(ep => ep.ProjectId == 2));
                db.Remove(projectToBeDeleted);
                db.SaveChanges();

                db.Projects
                .Select(p => p.Name)
                .Take(10)
                .ToList()
                .ForEach(p => FileWriter.WriteLine($"{p}"));
            }
        }
コード例 #8
0
        static void Main(string[] args)
        {
            using (SoftUniContext db = new SoftUniContext())
            {
                var projectToDelete = db.Projects.FirstOrDefault(p => p.ProjectId == 2);

                db.RemoveRange(db.EmployeesProjects
                               .Where(ep => ep.ProjectId == 2));
                db.Remove(projectToDelete);
                db.SaveChanges();

                using (StreamWriter Writer = new StreamWriter(@"..\..\..\result.txt"))
                {
                    foreach (var project in db.Projects.Take(10))
                    {
                        Writer.WriteLine($"{project.Name}");
                    }
                }
            }
        }
コード例 #9
0
        public static void Main(string[] args)
        {
            using (SoftUniContext dbContext = new SoftUniContext())
            {
                var projectToBeRemoved = dbContext.Projects.Where(p => p.ProjectId == 2).FirstOrDefault();

                var employeesRelatedToTheProjects = dbContext.EmployeesProjects.Where(e => e.ProjectId == 2);

                dbContext.RemoveRange(employeesRelatedToTheProjects);
                dbContext.Projects.Remove(projectToBeRemoved);
                dbContext.SaveChanges();

                var projects = dbContext.Projects.Take(10);

                foreach (var project in projects)
                {
                    Console.WriteLine($"{project.Name}");
                }
            }
        }
コード例 #10
0
        private static void RemoveAddresses(SoftUniContext context, int townId)
        {
            var addresses = context.Addresses
                            .Where(a => a.TownId == townId)
                            .ToArray();

            var town = context.Towns
                       .Where(t => t.TownId == townId)
                       .Select(t => t.Name)
                       .FirstOrDefault();

            int addressesCount = addresses.Length;

            context.RemoveRange(addresses);

            if (addressesCount > 1)
            {
                Console.WriteLine($"{addressesCount} addresses in {town} were deleted");
            }
            else if (addressesCount == 1)
            {
                Console.WriteLine($"{addressesCount} address in {town} was deleted");
            }
        }