static void CreateData() { using (FlugDbContext ctx = new FlugDbContext()) { ctx.Database.EnsureDeleted(); ctx.Database.EnsureCreated(); var f1 = new Flight { From = "Vienna", To = "London", Date = DateTime.Now }; var b1 = new Booking { Price = 300 }; f1.Bookings.Add(b1); ctx.Flights.Add(f1); ctx.SaveChanges(); #region addFurtherData var f2 = new Flight { From = "Vienna", To = "London", Date = DateTime.Now.AddHours(1) }; var f3 = new Flight { From = "Graz", To = "Hamburg", Date = DateTime.Now.AddHours(2) }; var f4 = new Flight { From = "Hamburg", To = "Graz", Date = DateTime.Now.AddHours(3) }; var f5 = new Flight { From = "Vienna", To = "London", Date = DateTime.Now.AddHours(4) }; var f6 = new Flight { From = "Graz", To = "Hamburg", Date = DateTime.Now.AddHours(5) }; var f7 = new Flight { From = "Vienna", To = "London", Date = DateTime.Now.AddHours(6) }; ctx.Flights.AddRange(f2, f3, f4, f5, f6); ctx.Flights.Add(f7); ctx.SaveChanges(); #endregion } }
// Mixed Server/Client-Evaluation static void ClientEval() { using (FlugDbContext ctx = new FlugDbContext()) { ctx.LogToConsole(); var flight = ctx.Flights .Where(f => f.Date >= DateTime.Today && CheckRoute(f, "Vienna-London")) .ToList(); // ... do stuff ... } }
static void EagerLoading() { using (FlugDbContext ctx = new FlugDbContext()) { var flight = ctx.Flights //.Include(f => f.Bookings.Select(b => b.Passenger)) .Include(f => f.Bookings) .ThenInclude(b => b.Passenger) .Where(f => f.From == "Vienna") .ToList(); // ... do stuff ... } }
static void Merge() { Flight flight; using (FlugDbContext ctx = new FlugDbContext()) { flight = ctx.Flights .Include(f => f.Bookings) .FirstOrDefault(f => f.Id == 1); } flight.Date = flight.Date.AddMinutes(30); using (FlugDbContext ctx = new FlugDbContext()) { ctx.ChangeTracker.TrackGraph(flight, (node) => { node.Entry.State = EntityState.Modified; }); ctx.SaveChanges(); } }
static void UpdateDetached() { Flight flight; using (FlugDbContext ctx = new FlugDbContext()) { flight = ctx.Flights .Include(f => f.Bookings) .FirstOrDefault(f => f.Id == 1); } flight.Date = flight.Date.AddMinutes(30); flight.Bookings.Add(new Booking { Price = 222 }); using (FlugDbContext ctx = new FlugDbContext()) { ctx.Flights.Update(flight); ctx.SaveChanges(); } }