public static void GetDeleteTown(SoftUniContext context) { Console.Write("Pease Enter a Town: "); string townName = Console.ReadLine(); var town = context.Towns.Where(t => t.Name == townName).First(); var addresess = context.Addresses .Where(a => a.Town.Name == townName).ToList(); foreach (var adr in addresess) { foreach (var emp in adr.Employees) { emp.AddressID = null; } context.Addresses.Remove(adr); context.SaveChanges(); } context.Towns.Remove(town); context.SaveChanges(); if (addresess.Count == 1) { Console.WriteLine($"1 address in {town.Name} was deleted"); } else { Console.WriteLine($"{addresess.Count} addresses in {town.Name} were deleted"); } }
public static void AddingAdressUpdateEmp(SoftUniContext context) { Address adress = new Address(); adress.AddressText = "Vitoshka 15"; adress.TownID = 4; context.Addresses.Add(adress); var employee = context.Employees.FirstOrDefault(e => e.LastName == "Nakov"); employee.Address = adress; context.SaveChanges(); var adresses = context.Employees.OrderByDescending(e => e.AddressID).Take(10).Select(e => e.Address.AddressText).ToList(); foreach (var adr in adresses) { Console.WriteLine(adr); } }
public static void GetDeletePrejctById(SoftUniContext context) { var project = context.Projects.Find(2); //.Where(p => p.ProjectID == 2); foreach (var emp in project.Employees) { emp.Projects.Remove(project); } context.Projects.Remove(project); context.SaveChanges(); var newPorjects = context.Projects.Take(10).Select(p => p.Name); foreach (var p in newPorjects) { Console.WriteLine(p); } }