예제 #1
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var teacher = new Teacher {
                    Name = "Susan Smith", School = "Custer Baker Middle School"
                };
                var firefighter = new Firefighter {
                    Name = "Joel Clark", FireStation = "Midtown"
                };
                var retired = new Retired {
                    Name = "Joan Collins", FullTimeHobby = "Scapbooking"
                };
                context.People.AddObject(teacher);
                context.People.AddObject(firefighter);
                context.People.AddObject(retired);
                context.SaveChanges();
                firefighter.Hero = teacher;
                teacher.Hero     = retired;
                retired.Hero     = firefighter;
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                foreach (var person in context.People)
                {
                    if (person.Hero != null)
                    {
                        Console.WriteLine("\n{0}, Hero is: {1}", person.Name, person.Hero.Name);
                    }
                    else
                    {
                        Console.WriteLine("{0}", person.Name);
                    }
                    if (person is Firefighter)
                    {
                        Console.WriteLine("Firefighter at station {0}", ((Firefighter)person).FireStation);
                    }
                    else if (person is Teacher)
                    {
                        Console.WriteLine("Teacher at {0}", ((Teacher)person).School);
                    }
                    else if (person is Retired)
                    {
                        Console.WriteLine("Retired, hobby is {0}", ((Retired)person).FullTimeHobby);
                    }
                    Console.WriteLine("Fans:");
                    foreach (var fan in person.Fans)
                    {
                        Console.WriteLine("\t{0}", fan.Name);
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
예제 #2
0
        /// <summary>
        /// Create a new Firefighter object.
        /// </summary>
        /// <param name="personId">Initial value of the PersonId property.</param>
        /// <param name="name">Initial value of the Name property.</param>
        public static Firefighter CreateFirefighter(global::System.Int32 personId, global::System.String name)
        {
            Firefighter firefighter = new Firefighter();

            firefighter.PersonId = personId;

            firefighter.Name = name;

            return(firefighter);
        }