Ejemplo n.º 1
0
 private static void Cleanup()
 {
     using (var context = new Recipe4Context())
     {
         context.Database.ExecuteSqlCommand("delete from chapter13.reservation");
     }
 }
Ejemplo n.º 2
0
 private static void Cleanup()
 {
     using (var context = new Recipe4Context())
     {
         context.Database.ExecuteSqlCommand("delete from chapter5.event");
         context.Database.ExecuteSqlCommand("delete from chapter5.club");
     }
 }
Ejemplo n.º 3
0
        private static void RunExample()
        {
            using (var context = new Recipe4Context())
            {
                context.Reservations.Add(new Reservation {
                    Name = "James Jordan", ResDate = DateTime.Parse("4/18/10")
                });
                context.Reservations.Add(new Reservation {
                    Name = "Katie Marlowe", ResDate = DateTime.Parse("3/22/10")
                });
                context.Reservations.Add(new Reservation {
                    Name = "Roger Smith", ResDate = DateTime.Parse("4/18/10")
                });
                context.Reservations.Add(new Reservation {
                    Name = "James Jordan", ResDate = DateTime.Parse("5/12/10")
                });
                context.Reservations.Add(new Reservation {
                    Name = "James Jordan", ResDate = DateTime.Parse("6/22/10")
                });
                context.SaveChanges();
            }

            using (var context = new Recipe4Context())
            {
                DateTime?searchDate = null;
                var      searchName = "James Jordan";

                Console.WriteLine("More complex SQL...");
                var query2 = from reservation in context.Reservations
                             let dateMatches = searchDate == null || reservation.ResDate == searchDate
                                               let nameMatches = searchName == string.Empty || reservation.Name.Contains(searchName)
                                                                 where dateMatches && nameMatches
                                                                 select reservation;
                foreach (var reservation in query2)
                {
                    Console.WriteLine("Found reservation for {0} on {1}", reservation.Name,
                                      reservation.ResDate.ToShortDateString());
                }

                Console.WriteLine("Cleaner SQL...");
                var query1 = from reservation in context.Reservations
                             where (searchDate == null || reservation.ResDate == searchDate)
                             &&
                             (searchName == string.Empty || reservation.Name.Contains(searchName))
                             select reservation;
                foreach (var reservation in query1)
                {
                    Console.WriteLine("Found reservation for {0} on {1}", reservation.Name,
                                      reservation.ResDate.ToShortDateString());
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Ejemplo n.º 4
0
        private static void RunExample()
        {
            int desertSunId;

            using (var context = new Recipe4Context())
            {
                var starCity = new Club {
                    Name = "Star City Chess Club", City = "New York"
                };
                var desertSun = new Club {
                    Name = "Desert Sun Chess Club", City = "Phoenix"
                };
                var palmTree = new Club {
                    Name = "Palm Tree Chess Club", City = "San Diego"
                };

                context.Clubs.Add(starCity);
                context.Clubs.Add(desertSun);
                context.Clubs.Add(palmTree);

                context.SaveChanges();

                desertSunId = desertSun.ClubId;
            }

            using (var context = new Recipe4Context())
            {
                Console.WriteLine("\nLocal Collection Behavior");
                Console.WriteLine("=================");

                Console.WriteLine("\nNumber of Clubs Contained in Local Collection: {0}", context.Clubs.Local.Count);
                Console.WriteLine("=================");

                Console.WriteLine("\nClubs Retrieved from Context Object");
                Console.WriteLine("=================");
                foreach (var club in context.Clubs.Take(2))
                {
                    Console.WriteLine("{0} is located in {1}", club.Name, club.City);
                }

                Console.WriteLine("\nClubs Contained in Context Local Collection");
                Console.WriteLine("=================");
                foreach (var club in context.Clubs.Local)
                {
                    Console.WriteLine("{0} is located in {1}", club.Name, club.City);
                }

                context.Clubs.Find(desertSunId);

                Console.WriteLine("\nClubs Retrieved from Context Object - Revisted");
                Console.WriteLine("=================");
                foreach (var club in context.Clubs)
                {
                    Console.WriteLine("{0} is located in {1}", club.Name, club.City);
                }

                Console.WriteLine("\nClubs Contained in Context Local Collection - Revisted");
                Console.WriteLine("=================");
                foreach (var club in context.Clubs.Local)
                {
                    Console.WriteLine("{0} is located in {1}", club.Name, club.City);
                }

                // Get reference to local observable collection
                var localClubs = context.Clubs.Local;

                // Add new Club
                var lonesomePintId = -999;
                localClubs.Add(new Club
                {
                    City   = "Portland",
                    Name   = "Lonesome Pine",
                    ClubId = lonesomePintId
                });

                // Remove Desert Sun club
                localClubs.Remove(context.Clubs.Find(desertSunId));

                Console.WriteLine("\nClubs Contained in Context Object - After Adding and Deleting");
                Console.WriteLine("=================");
                foreach (var club in context.Clubs)
                {
                    Console.WriteLine("{0} is located in {1} with a Entity State of {2}",
                                      club.Name, club.City, context.Entry(club).State);
                }

                Console.WriteLine("\nClubs Contained in Context Local Collection - After Adding and Deleting");
                Console.WriteLine("=================");
                foreach (var club in localClubs)
                {
                    Console.WriteLine("{0} is located in {1} with a Entity State of {2}",
                                      club.Name, club.City, context.Entry(club).State);
                }

                Console.WriteLine("\nPress <enter> to continue...");
                Console.ReadLine();
            }
        }