Exemplo n.º 1
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var d1 = new Dealer {
                    Name = "All Cities Toyota"
                };
                var d2 = new Dealer {
                    Name = "Southtown Toyota"
                };
                var d3 = new Dealer {
                    Name = "Luxury Auto World"
                };
                var c1 = new Toyota {
                    Model = "Camry", Color = "Green", Year = "2010", Dealer = d1
                };
                var c2 = new BMW {
                    Model = "310i", Color = "Blue", CollisionAvoidance = true, Year = "2010", Dealer = d3
                };
                var c3 = new Toyota {
                    Model = "Tundra", Color = "Blue", Year = "2010", Dealer = d2
                };
                context.Dealers.AddObject(d1);
                context.Dealers.AddObject(d2);
                context.Dealers.AddObject(d3);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                Console.WriteLine("Dealers and Their Cars");
                Console.WriteLine("======================");
                foreach (var dealer in context.Dealers)
                {
                    Console.WriteLine("\nDealer: {0}", dealer.Name);
                    foreach (var car in dealer.Cars)
                    {
                        string make = string.Empty;
                        if (car is Toyota)
                        {
                            make = "Toyota";
                        }
                        else if (car is BMW)
                        {
                            make = "BMW";
                        }
                        Console.WriteLine("\t{0} {1} {2} {3}", car.Year, car.Color, make, car.Model);
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        /// <summary>
        /// Create a new Toyota object.
        /// </summary>
        /// <param name="carId">Initial value of the CarId property.</param>
        /// <param name="model">Initial value of the Model property.</param>
        /// <param name="year">Initial value of the Year property.</param>
        /// <param name="color">Initial value of the Color property.</param>
        public static Toyota CreateToyota(global::System.Int32 carId, global::System.String model, global::System.String year, global::System.String color)
        {
            Toyota toyota = new Toyota();

            toyota.CarId = carId;

            toyota.Model = model;

            toyota.Year = year;

            toyota.Color = color;

            return(toyota);
        }