コード例 #1
0
 private static void Cleanup()
 {
     using (var context = new Recipe3Context())
     {
         context.Database.ExecuteSqlCommand("delete from chapter13.appointment");
         context.Database.ExecuteSqlCommand("delete from chapter13.doctor");
         context.Database.ExecuteSqlCommand("delete from chapter13.company");
     }
 }
コード例 #2
0
 private static void Cleanup()
 {
     using (var context = new Recipe3Context())
     {
         // Clear out prior test data
         context.Database.ExecuteSqlCommand("delete from chapter5.event");
         context.Database.ExecuteSqlCommand("delete from chapter5.club");
     }
 }
コード例 #3
0
        private static void RunExample()
        {
            using (var context = new Recipe3Context())
            {
                var company = new Company {
                    Name = "Paola Heart Center"
                };
                var doc1 = new Doctor {
                    Name = "Jill Mathers", Company = company
                };
                var doc2 = new Doctor {
                    Name = "Robert Stevens", Company = company
                };
                var app1 = new Appointment
                {
                    AppointmentDate = DateTime.Parse("3/18/2010"),
                    Patient         = "Karen Rodgers",
                    Doctor          = doc1
                };
                var app2 = new Appointment
                {
                    AppointmentDate = DateTime.Parse("3/20/2010"),
                    Patient         = "Steven Cook",
                    Doctor          = doc2
                };
                context.Doctors.Add(doc1);
                context.Doctors.Add(doc2);
                context.Appointments.Add(app1);
                context.Appointments.Add(app2);
                context.Companies.Add(company);
                context.SaveChanges();
            }

            using (var context = new Recipe3Context())
            {
                Console.WriteLine("Entities tracked in context for Doctors...");

                // execute query using the AsNoTracking() method
                context.Doctors.Include("Company").AsNoTracking().ToList();

                Console.WriteLine("Number of entities loaded into context with AsNoTracking: {0}",
                                  context.ChangeTracker.Entries().Count());

                // execute query without the AsNoTracking() method
                context.Doctors.Include("Company").ToList();

                Console.WriteLine("Number of entities loaded into context without AsNoTracking: {0}",
                                  context.ChangeTracker.Entries().Count());
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
コード例 #4
0
        private static void RunExample()
        {
            int starCityId;
            int desertSunId;
            int palmTreeId;

            using (var context = new Recipe3Context())
            {
                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();

                // SaveChanges() returns newly created Id value for each club
                starCityId  = starCity.ClubId;
                desertSunId = desertSun.ClubId;
                palmTreeId  = palmTree.ClubId;
            }

            using (var context = new Recipe3Context())
            {
                var starCity = context.Clubs.SingleOrDefault(x => x.ClubId == starCityId);
                starCity = context.Clubs.SingleOrDefault(x => x.ClubId == starCityId);
                starCity = context.Clubs.Find(starCityId);
                var desertSun = context.Clubs.Find(desertSunId);
                var palmTree  = context.Clubs.AsNoTracking().SingleOrDefault(x => x.ClubId == palmTreeId);
                palmTree = context.Clubs.Find(palmTreeId);
                var lonesomePintId = -999;
                context.Clubs.Add(new Club {
                    City = "Portland", Name = "Lonesome Pine", ClubId = lonesomePintId,
                });
                var lonesomePine    = context.Clubs.Find(lonesomePintId);
                var nonexistentClub = context.Clubs.Find(10001);
            }

            Console.WriteLine("Please run this application using SQL Server Profiler...");
            Console.ReadLine();
        }