Пример #1
0
        /// <summary>
        /// requirement: No instantiation of List<Booking> in Flight ctor
        /// </summary>
        public static void DemoFillNavigationProperties()
        {
            var ctx = new WWWingsContext();

            Console.WriteLine("---------- Load flight");
            var flight101 = ctx.FlightSet.Where(x => x.FlightNo == 101).SingleOrDefault();

            Console.WriteLine("BookingSet: " + flight101.BookingSet?.Count);
            Console.WriteLine("---------- New flight");
            var flightNeu = new BO.Flight();

            Console.WriteLine("BookingSet: " + flightNeu.BookingSet?.Count);
        }
Пример #2
0
        public static void Demo2_RueckwartigeBeziehung()
        {
            int id = 250;

            // Vorspiel
            using (var ctx = new WWWingsContext())
            {
                var falt = ctx.FlightSet.Find(id);
                if (falt != null)
                {
                    ctx.FlightSet.Remove(falt); ctx.SaveChanges();
                }
            }

            using (var ctx = new WWWingsContext())
            {
                var falt = ctx.FlightSet.Find(id);
                if (falt != null)
                {
                    ctx.FlightSet.Remove(falt); ctx.SaveChanges();
                }
                CUI.Print("Neuer Flight", ConsoleColor.Yellow);
                var f = new BO.Flight();
                f.FlightNo    = id;
                f.Departure   = "Frankfurt";
                f.Destination = "Berlin";
                f.Date        = DateTime.Now;
                f.FreeSeats   = 10;
                f.Seats       = 100;

                ctx.FlightSet.Add(f);
                Console.WriteLine(f);

                CUI.Print("Neuer Pilot", ConsoleColor.Yellow);
                var p = new BO.Pilot();
                p.Surname          = "Schwichtenberg";
                p.GivenName        = "Holger";
                p.PilotLicenseType = PilotLicenseType.ATP;
                ctx.PilotSet.Add(p);
                Console.WriteLine(p);

                Console.WriteLine("--- Navigationseigenschaften-Mengentypen");
                Console.WriteLine(p.FlightAsPilotSet?.Count + " FlightAsPilotSet: " + p.FlightAsPilotSet?.GetType().FullName);
                Console.WriteLine(p.FlightAsCopilotSet?.Count + " FlightAsCopilotSet: " + p.FlightAsCopilotSet?.GetType().FullName);
                p.LicenseDate = DateTime.Now;

                CUI.Print("Zuweisung des Pilots zum Flight", ConsoleColor.Yellow);
                f.Pilot   = p;
                f.Copilot = p;

                Console.WriteLine("--- Navigationseigenschaften-Mengentypen");
                Console.WriteLine(p.FlightAsPilotSet?.Count + " FlightAsPilotSet: " + p.FlightAsPilotSet?.GetType().FullName);
                Console.WriteLine(p.FlightAsCopilotSet?.Count + " FlightAsCopilotSet: " + p.FlightAsCopilotSet?.GetType().FullName);

                CUI.Print("Speichern und Relationship Fixup", ConsoleColor.Yellow);
                ctx.SaveChanges();
                ctx.ChangeTracker.DetectChanges();

                // to SaveChanges sind rückwärtige Beziehungen hergestellt
                Console.WriteLine("--- Navigationseigenschaften-Mengentypen");
                Console.WriteLine(p.FlightAsPilotSet?.Count + " FlightAsPilotSet: " + p.FlightAsPilotSet?.GetType().FullName);
                Console.WriteLine(p.FlightAsCopilotSet?.Count + " FlightAsCopilotSet: " + p.FlightAsCopilotSet?.GetType().FullName);
            }
        }