static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter13.member"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var ceo = new CEO { Name = "Joan Miller" }; var super = new Supervisor { Name = "Bill Mayer", Manager = ceo }; var pm = new ProjectManager { Name = "Jill Williams", Manager = super }; context.Associates.AddObject(ceo); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var ceo = context.Associates.First(a => a.ReportsTo == null); var associates = context.Associates.ToList(); PrintDetails(ceo); } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var app1 = new Appointment { StartsAt = DateTime.Parse("4/7/2010 14:00"), GoesTo = DateTime.Parse("4/7/2010 15:00") }; var app2 = new Appointment { StartsAt = DateTime.Parse("4/8/2010 9:00"), GoesTo = DateTime.Parse("4/8/2010 11:00") }; var app3 = new Appointment { StartsAt = DateTime.Parse("4/8/2010 13:00"), GoesTo = DateTime.Parse("4/7/2010 15:00") }; context.Appointments.AddObject(app1); context.Appointments.AddObject(app2); context.Appointments.AddObject(app3); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { var apps = from a in context.Appointments where SqlFunctions.DatePart("WEEKDAY", a.StartsAt) == 5 select a; Console.WriteLine("Appointments for Thursday"); Console.WriteLine("========================="); foreach (var appointment in apps) { Console.WriteLine("Appointment from {0} to {1}", appointment.StartsAt.ToShortTimeString(), appointment.GoesTo.ToShortTimeString()); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter2.park"); context.ExecuteStoreCommand("delete from chapter2.location"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter3.accident"); context.ExecuteStoreCommand("delete from chapter3.worker"); } }
static void RunExample() { using (var context = new EFRecipesEntities()) { var pc = new Customer { Name = "Steven James" }; var rc = new Customer { Name = "Kathy Naudot" }; pc.PreferredOrders.Add(new Order { Amount = 19.95M }); pc.CustomerDiscount = new CustomerDiscount { PurchaseDiscount = 10 }; rc.RiskyOrders.Add(new Order { Amount = 29.99M }); rc.CreditReports.Add(new CreditReport { CreditRating = 630 }); context.PreferredCustomers.AddObject(pc); context.RiskyCustomers.AddObject(rc); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Preferred Customers"); context.ContextOptions.LazyLoadingEnabled = true; foreach (var customer in context.PreferredCustomers) { Console.WriteLine("Name: {0}, Discount = {1}%", customer.Name, customer.CustomerDiscount.PurchaseDiscount.ToString()); foreach (var order in customer.PreferredOrders) { Console.WriteLine("\tOrder: {0}", order.Amount.ToString("C")); } } Console.WriteLine("\nRisky Customers"); foreach (var customer in context.RiskyCustomers) { Console.WriteLine("Name: {0}", customer.Name); foreach (var order in customer.RiskyOrders) { Console.WriteLine("\tOrder: {0}", order.Amount.ToString("C")); } foreach (var report in customer.CreditReports) { Console.WriteLine("\tCredit Score: {0}", report.CreditRating.ToString()); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter5.ceo"); context.ExecuteStoreCommand("delete from chapter5.projectmanager"); context.ExecuteStoreCommand("delete from chapter5.supervisor"); context.ExecuteStoreCommand("delete from chapter5.associate"); } }
static void Cleanup() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter15.creditreport"); context.ExecuteStoreCommand("delete from chapter15.creditriskorder"); context.ExecuteStoreCommand("delete from chapter15.creditriskcustomer"); context.ExecuteStoreCommand("delete from chapter15.preferredOrder"); context.ExecuteStoreCommand("delete from chapter15.customerdiscount"); context.ExecuteStoreCommand("delete from chapter15.preferredcustomer"); } }
static void Main(string[] args) { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand("delete from chapter6.weborder"); } using (var context = new EFRecipesEntities()) { var order = new WebOrder { CustomerName = "Jim Allen", OrderDate = DateTime.Parse("5/3/2009"), IsDeleted = false, Amount = 200 }; context.WebOrders.AddObject(order); order = new WebOrder { CustomerName = "John Stevens", OrderDate = DateTime.Parse("1/1/2006"), IsDeleted = false, Amount = 400 }; context.WebOrders.AddObject(order); order = new WebOrder { CustomerName = "Russel Smith", OrderDate = DateTime.Parse("1/3/2006"), IsDeleted = true, Amount = 500 }; context.WebOrders.AddObject(order); order = new WebOrder { CustomerName = "Mike Hammer", OrderDate = DateTime.Parse("3/6/2006"), IsDeleted = true, Amount = 1800 }; context.WebOrders.AddObject(order); order = new WebOrder { CustomerName = "Steve Jones", OrderDate = DateTime.Parse("1/1/2003"), IsDeleted = true, Amount = 600 }; context.WebOrders.AddObject(order); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { Console.WriteLine("Orders"); Console.WriteLine("======"); foreach (var order in context.WebOrders) { Console.WriteLine("\nCustomer: {0}", order.CustomerName); Console.WriteLine("OrderDate: {0}", order.OrderDate.ToShortDateString()); Console.WriteLine("Is Deleted: {0}", order.IsDeleted.ToString()); Console.WriteLine("Amount: {0}", order.Amount.ToString("C")); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var park = new Park { Name = "11th Street Park", Address = "801 11th Street", City = "Aledo", State = "TX", ZIPCode = "76106" }; var loc = new Location { Address = "501 Main", City = "Weatherford", State = "TX", ZIPCode = "76201" }; park.Office = loc; context.Locations.AddObject(park); park = new Park { Name = "Overland Park", Address = "101 High Drive", City = "Springtown", State = "TX", ZIPCode = "76081" }; loc = new Location { Address = "8705 Range Lane", City = "Springtown", State = "TX", ZIPCode = "76081" }; park.Office = loc; context.Locations.AddObject(park); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = true; Console.WriteLine("-- All Locations -- "); foreach (var l in context.Locations) { Console.WriteLine("{0}, {1}, {2} {3}", l.Address, l.City, l.State, l.ZIPCode); } Console.WriteLine("--- Parks ---"); foreach (var p in context.Locations.OfType <Park>()) { Console.WriteLine("{0} is at {1} in {2}", p.Name, p.Address, p.City); Console.WriteLine("\tOffice: {0}, {1}, {2} {3}", p.Office.Address, p.Office.City, p.Office.State, p.Office.ZIPCode); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.ParkingTickets.AddObject(new ParkingTicket { Amount = 132.0M, Paid = false }); context.ParkingTickets.AddObject(new ParkingTicket { Amount = 255.0M, Paid = false }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { foreach (var ticket in context.ParkingTickets) { Console.WriteLine("Ticket: {0}", ticket.TicketId); Console.WriteLine("Date: {0}", ticket.CreateDate.ToShortDateString()); Console.WriteLine("Amount: {0}", ticket.Amount.ToString("C")); Console.WriteLine("Paid: {0}", ticket.PaidDate.HasValue ? ticket.PaidDate.Value.ToShortDateString() : "Not Paid"); Console.WriteLine(); ticket.Paid = true; // just paid ticket! } // save all those Paid flags context.SaveChanges(); foreach (var ticket in context.ParkingTickets) { Console.WriteLine("Ticket: {0}", ticket.TicketId); Console.WriteLine("Date: {0}", ticket.CreateDate.ToShortDateString()); Console.WriteLine("Amount: {0}", ticket.Amount.ToString("C")); Console.WriteLine("Paid: {0}", ticket.PaidDate.HasValue ? ticket.PaidDate.Value.ToShortDateString() : "Not Paid"); Console.WriteLine(); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { context.ExecuteStoreCommand(@"insert into chapter13.member(name,salary) values ('Steven Jones',45000)"); context.ExecuteStoreCommand(@"insert into chapter13.member(name,salary) values ('Kathy Kurtz', 85000)"); context.ExecuteStoreCommand(@"insert into chapter13.member(name,salary) values ('Aaron McCabe', 82000)"); } using (var context = new EFRecipesEntities()) { var upperclass = context.Members.OfType <UpperClass>(); foreach (var member in upperclass) { Console.WriteLine("{0}", member.Name); } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }
static void RunExample() { using (var context = new EFRecipesEntities()) { var worker1 = new Worker { Name = "John Kearney" }; var worker2 = new Worker { Name = "Nancy Roberts" }; var worker3 = new Worker { Name = "Karla Gibbons" }; context.Accidents.AddObject(new Accident { Description = "Cuts and contusions", Severity = 3, Worker = worker1 }); context.Accidents.AddObject(new Accident { Description = "Broken foot", Severity = 4, Worker = worker1 }); context.Accidents.AddObject(new Accident { Description = "Fall, no injuries", Severity = 1, Worker = worker2 }); context.Accidents.AddObject(new Accident { Description = "Minor burn", Severity = 3, Worker = worker2 }); context.Accidents.AddObject(new Accident { Description = "Back strain", Severity = 2, Worker = worker3 }); context.SaveChanges(); } using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = false; var query = from w in context.Workers select new { Worker = w, Accidents = w.Accidents.Where(a => a.Severity > 2) }; query.ToList(); var workers = query.Select(r => r.Worker); Console.WriteLine("Workers with serious accidents..."); foreach (var worker in workers) { Console.WriteLine("{0} had the following accidents", worker.Name); if (worker.Accidents.Count == 0) { Console.WriteLine("\t--None--"); } foreach (var accident in worker.Accidents) { Console.WriteLine("\t{0}, severity: {1}", accident.Description, accident.Severity.ToString()); } } } Console.WriteLine(); using (var context = new EFRecipesEntities()) { context.ContextOptions.LazyLoadingEnabled = false; foreach (var worker in context.Workers) { Console.WriteLine("{0} had the following accidents", worker.Name); var accidents = worker.Accidents.CreateSourceQuery().Where(a => a.Severity > 2); worker.Accidents.Attach(accidents); if (worker.Accidents.Count == 0) { Console.WriteLine("\t--None--"); } foreach (var accident in accidents) { Console.WriteLine("\t{0}, severity: {1}", accident.Description, accident.Severity.ToString()); } } } Console.WriteLine("Press <enter> to continue..."); Console.ReadLine(); }