public static void AddFlightWithDetails() { Console.WriteLine(nameof(AddFlightWithDetails)); using (WWWingsContext ctx = new WWWingsContext()) { //var p = ctx.PilotSet.SingleOrDefault(x => x.PersonID == 234); // Flight im RAM anlegen var f = new Flight(); f.FlightNo = 123456; //f.Pilot = p; f.Departure = "Essen"; f.Destination = "Sydney"; f.AirlineCode = "WWW"; f.PilotId = 23; f.Copilot = null; // kein Copilot :-( f.Seats = 100; f.FreeSeats = 100; Console.WriteLine($"Vor dem Anfügen: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State); // Flight dem Kontext "hinzufügen" ctx.FlightSet.Add(f); ctx.FlightSet.Add(f); ctx.Add(f); Console.WriteLine($"Nach dem Anfügen: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State); try { var anz = ctx.SaveChanges(); if (anz == 0) { Console.WriteLine("Problem: No changes saved!"); } else { Console.WriteLine("Number of saved changes: " + anz); } Console.WriteLine($"After saving: Flight #{f.FlightNo}: {f.Departure}->{f.Destination} has {f.FreeSeats} free seats! State of the flight object: " + ctx.Entry(f).State); } catch (Exception ex) { Console.WriteLine("Error: " + ex.ToString()); } } }
public void Add(Flight f) { ctx.Add(f); }