Esempio n. 1
0
        private static void DeleteAnOwnerWithAllCarsTheyOwn()
        {
            // 2. show all owners, choose an owner, empty means set owner to null
            ListAllOwnersAndTheirCars();
            Console.Write("Enter Id of an owner");
            int   ownerId = int.Parse(Console.ReadLine()); // FIXME
            Owner owner   = ctx.Owners.Find(ownerId);

            if (owner == null)
            {
                Console.WriteLine("Owner not found");
                return;
            }
            //
            foreach (Car c in owner.CarsCollection.ToList <Car>())
            {
                ctx.Cars.Remove(c); // schedule for deletion
            }
            ctx.Owners.Remove(owner);
            ctx.SaveChanges();
            Console.WriteLine("Owner and their cars deleted");
        }
Esempio n. 2
0
        static void AddCar()
        {
            Console.WriteLine("Please, enter the make and model:");
            string makeModel = Console.ReadLine();

            Console.WriteLine("Please, enter the year of production:");
            string yearStr = Console.ReadLine();

            if (!int.TryParse(yearStr, out int year))
            {
                Console.WriteLine("The year of production should be a valid integer");
                return;
            }
            try
            {
                Car car = new Car()
                {
                    MakeModel = makeModel, YearOfProd = year
                };
                ctx.Cars.Add(car);
                ctx.SaveChanges();
                Console.WriteLine("Person added.");
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("Person data invalid: " + ex.Message);
            }
            catch (DataException ex)
            {
                Console.WriteLine("Database problem: " + ex.Message);
            }
            catch (SystemException ex)
            {
                Console.WriteLine("Database problem: " + ex.Message);
            }
        }