Example #1
0
        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();
        }
Example #3
0
        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())
            {
                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();
        }