Ejemplo n.º 1
0
 static void ShowCars()
 {
     using (var context = new CarsContext())
     {
         var cars = context.Cars;
         foreach (var car in cars)
         {
             Console.WriteLine($"{car.Model} \t {car.Price}");
         }
     }
 }
Ejemplo n.º 2
0
        static void AddCar()
        {
            var car = new Car();

            Console.WriteLine("Enter Price");
            car.Price = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Model");
            car.Model = Console.ReadLine();
            Console.WriteLine("Enter Color");
            car.Color    = Console.ReadLine();
            car.DateMade = DateTime.Now;

            using (var context = new CarsContext())
            {
                Console.WriteLine("Select Dealer");
                Console.WriteLine("0 - no dealer");
                foreach (var dealer in context.Dealers)
                {
                    Console.WriteLine($"{dealer.Id} - {dealer.Name}");
                }
                var dealerId = Int32.Parse(Console.ReadLine());
                if (dealerId != 0)
                {
                    var dealer = context.Dealers.Find(dealerId);
                    if (dealer != null)
                    {
                        dealer.Cars.Add(car);
                    }
                    context.SaveChanges();
                }
                else
                {
                    context.Cars.Add(car);
                    context.SaveChanges();
                }
            }
        }