void GivenBrendanHasActivities()
        {
            DropDatabase.DropDb();
            using (var context = new DemoContextFactory().Create())
            {
                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                Assert.NotNull(brendan);

                brendan.Activities.Add(new InSchoolActivity()
                {
                    Name="Computer Club",
                    ClassRoom = "1A"
                });
                brendan.Activities.Add(new InSchoolActivity()
                {
                    Name = "Music Lessons",
                    ClassRoom = "C#"
                });
                brendan.Activities.Add(new OutsideSchoolActivity()
                {
                    Name = "Rowing Club",
                    ResponsiblePerson = "Mr Cox"
                });

                context.SaveChanges();
            }
        }
        public void DoManyToManyTest()
        {

            DropDatabase.DropDb();

            using (var context = new DemoContextFactory().Create())
            {
                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");
                var adam = context.Students.FirstOrDefault(s => s.FirstName == "Adam");

                var course = context.Courses.FirstOrDefault(c => c.Title == "How To Win");

                Assert.NotNull(brendan);
                Assert.NotNull(adam);
                Assert.NotNull(course);

                course.Students.Add(brendan);
                course.Students.Add(adam);

                context.SaveChanges();
            }


            using (var context2 = new DemoContextFactory().Create())
            {
                var course = context2.Courses.FirstOrDefault(c => c.Title == "How To Win");

                Assert.Equal(2, course.Students.Count());
            }

        }
 void ThenICanQueryAll()
 {
     using (var context = new DemoContextFactory().Create())
     {
         var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");
         Assert.NotNull(brendan.Activities);
         Assert.Equal(3, brendan.Activities.Count());
     }
 }
        void ThenEnrolmentsIsNullWithLazyLoadingOff()
        {
            using (var context = new DemoContextFactory().Create())
            {
                context.Configuration.LazyLoadingEnabled = false;

                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");
                Assert.Null(brendan.Enrolments);
            }
        }
        void AndThenICanQueryByType()
        {
            using (var context = new DemoContextFactory().Create())
            {
                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                var inSchoolActivities = brendan.Activities.OfType<InSchoolActivity>();
                Assert.Equal(2, inSchoolActivities.Count());

                var outsideSchoolActivities = brendan.Activities.OfType<OutsideSchoolActivity>();
                Assert.Equal(1, outsideSchoolActivities.Count());
            }
        }
        void ThenEnrolmentsareLazyLoaded()
        {
            using (var context = new DemoContextFactory().Create())
            {
                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                // put breakpoint here
                Assert.NotNull(brendan);
                Assert.NotEmpty(brendan.Enrolments);
                Console.Out.WriteLine(string.Format("entity type {0}", brendan.GetType().FullName));
                Console.Out.WriteLine(string.Format("collection Type: {0}",brendan.Enrolments.GetType().FullName));
                
            }
        }
        void ThenEnrolmentsareLazyLoaded()
        {
            Student brendan;

            using (var context = new DemoContextFactory().Create())
            {
                brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");    
            }

            // put breakpoint here
            Assert.NotNull(brendan);
            Assert.NotNull(brendan.Enrolments);
            Assert.NotEmpty(brendan.Enrolments);
        }
        void ThenReattachedEntitiesCanBeLazyLoadedLazyLoaded()
        {
            Student brendan;

            using (var context = new DemoContextFactory().Create())
            {
                brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");    
            }

            using (var context = new DemoContextFactory().Create())
            {
                context.Students.Attach(brendan);
                Assert.NotEmpty(brendan.Enrolments);
            }
        }
        void ThenICanManuallyLoadReferences()
        {
            using (var context = new DemoContextFactory().Create())
            {
                context.Configuration.LazyLoadingEnabled = false;

                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                Assert.Null(brendan.Enrolments);

                context.Entry(brendan).Collection(s => s.Enrolments).Load();

                Assert.NotNull(brendan.Enrolments);
                Assert.NotEmpty(brendan.Enrolments);
            }
        }
        void WhenIUseIncludeEnrolmentsAreProvided()
        {
            using (var context = new DemoContextFactory().Create())
            {
                context.Configuration.LazyLoadingEnabled = false;

                var brendan = context.Students
                    .Include(s => s.Enrolments)
                    .Include(s => s.Courses)
                    .Include(s => s.Courses.Select(c => c.School))
                    .FirstOrDefault(s => s.FirstName == "Brendan");

                Assert.NotNull(brendan.Enrolments);
                Assert.NotEmpty(brendan.Enrolments);
            }
        }
        public void DoOneToManyTest()
        {

            DropDatabase.DropDb();

            using (var context = new DemoContextFactory().Create())
            {
                var student = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                Assert.NotNull(student);

                student.Enrolments.Add(new Enrolment()
                {
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddYears(1),
                    School = context.Schools.FirstOrDefault(s => s.Name == "School Of Rock")
                });

                context.SaveChanges();

            }
        }
        void GivenBrendanHasEnrolments()
        {
            DropDatabase.DropDb();
            using (var context = new DemoContextFactory().Create())
            {
                var brendan = context.Students.FirstOrDefault(s => s.FirstName == "Brendan");

                Assert.NotNull(brendan);

                brendan.Enrolments.Add(new Enrolment()
                {
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddYears(1)
                });

                brendan.Enrolments.Add(new Enrolment()
                {
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddYears(1)
                });

                context.SaveChanges();
            }
        }