static Car UpdateRandomCar(CarBoundContext context) { var randomCar = context.Cars.GetRandomElement(); randomCar.Model += $" (modified {DateTime.Now}"; context.SaveChanges(); return(randomCar); }
static Car CreateNewCar(CarBoundContext context) { var newCar = _carFactory.CreateNewRandomCar(); context.Cars.Add(newCar); context.SaveChanges(); return(newCar); }
static void RemoveAllCars(CarBoundContext context) { var cars = context.Cars; var count = cars.Count(); cars.RemoveRange(cars); context.SaveChanges(); Console.Clear(); Console.WriteLine($"Removed {count} cars from repository."); PressAnyKey(); }
static void Main(string[] args) { // The database will be created at bin/Debug/ by default AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine("c:\\temp\\", "")); // Get a new context for de DB (always "using" to dispose the connection) using (var context = new CarBoundContext()) { do { switch (ShowMenu(context.Cars.Count())) { case ConsoleKey.D1: var newCar = CreateNewCar(context); ShowNewCar(newCar); break; case ConsoleKey.D2: var cars = GetCars(context); ShowCars(cars); break; case ConsoleKey.D3: var updatedCar = UpdateRandomCar(context); ShowUpdatedCar(updatedCar); break; case ConsoleKey.D4: RemoveAllCars(context); break; case ConsoleKey.D0: return; } Console.Clear(); } while (true); } }
static IEnumerable <Car> GetCars(CarBoundContext context) { return(context.Cars); }