コード例 #1
0
ファイル: StartUp.cs プロジェクト: mkpetrov/EFDatabaseFirst
        public static string RemoveTown(SoftUniContext context)
        {
            var seattleTown = context.Towns.First(x => x.Name == "Seattle");
            var addresses   = context.Addresses.Where(x => x.Town.TownId == seattleTown.TownId).ToList();

            var result = $"{addresses.Count()} addresses in Seattle were deleted";

            var addressesIds     = addresses.Select(x => x.AddressId).ToList();
            var seattleEmployees = context.Employees.Where(x => addressesIds.Contains(x.AddressId.Value)).ToList();

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

            context.UpdateRange(seattleEmployees);
            context.RemoveRange(addresses);
            context.Remove(seattleTown);

            return(result);
        }
コード例 #2
0
ファイル: StartUp.cs プロジェクト: mkpetrov/EFDatabaseFirst
        public static string DeleteProjectById(SoftUniContext context)
        {
            var deleteProject     = context.Projects.First(x => x.ProjectId == 2);
            var project2Employees = context.EmployeesProjects.Where(x => x.ProjectId == 2).ToList();
            var emplIds           = project2Employees.Select(x => x.EmployeeId).ToList();
            var employees         = context.Employees.Where(x => emplIds.Contains(x.EmployeeId)).ToList();

            employees.ForEach(e => e.EmployeesProjects = null);
            context.UpdateRange(employees);
            context.RemoveRange(project2Employees);
            context.Remove(deleteProject);
            context.SaveChanges();

            var projects = context.Projects.Take(10).ToList();

            var sb = new StringBuilder();

            foreach (var project in projects)
            {
                sb.AppendLine(project.Name);
            }

            return(sb.ToString().Trim());
        }