public void Vaidate_On_Demand_Fails()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                Speaker speaker = new Speaker();

                var validationResult = context.Entry(speaker).GetValidationResult().IsValid;

                TestHelpers.WriteValiationResults(context, speaker);
                //Console.WriteLine("Speaker is {0}",
                //                  validationResult ? "Valid" : "Invalid");

                Assert.IsFalse(validationResult);

            }
        }
        public void Validate_Invdividual_Properties()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                Speaker speaker = new Speaker();

                Console.WriteLine("Email validation check 1");
                TestHelpers.WritePropertyValidationResults(context,speaker,"Email");

                Console.WriteLine("\nEmail validation check 2");
                speaker.Email = "bademail.com";
                TestHelpers.WritePropertyValidationResults(context, speaker, "Email");

                Console.WriteLine("\nEmail validation check 3");
                speaker.Email = "*****@*****.**";
                TestHelpers.WritePropertyValidationResults(context, speaker, "Email","FirstName");
            }
        }
        public void Vaidate_On_Demand_Passes()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                Speaker speaker = new Speaker
                                      {
                                          FirstName = "Test",
                                          LastName = "Speaker",
                                          Email = "*****@*****.**"
                                      };

                var validationResult = context.Entry(speaker).GetValidationResult().IsValid;

                Console.WriteLine("Speaker is {0}",
                                  validationResult ? "Valid" : "Invalid");

                Assert.IsTrue(validationResult);
            }
        }
        public void Validate_Multiple_Entities()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                Speaker speaker = new Speaker
                                      {
                                          FirstName = "Test",
                                          Presentations = new Collection<Presentation>(new[] {new Presentation()})
                                      };

                Presentation presentation = new Presentation {Speaker = new Speaker()};

                CodeCampEvent codeCamp = new CodeCampEvent
                                             {
                                                 Topics = new Collection<EventSession>(new []{new EventSession{Presentation = presentation} })
                                             };

                context.Speakers.Add(speaker);

                context.CodeCampEvents.Add(codeCamp);

                TestHelpers.WriteValiationResults(context);
            }
        }
        public void Local_Data_Allows_Queries_Against_New_Records_Not_Saved_To_Database()
        {
            using(var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var speaker = new Speaker
                                  {
                                      FirstName = "Test",
                                      LastName = "User01",
                                      Email = "*****@*****.**"
                                  };

                context.Speakers.Add(speaker);

                // query to database will result in a null record
                var query1 = context.Speakers.SingleOrDefault(s => s.Email == "*****@*****.**");
                Assert.IsNull(query1);

                // query of local data will return new record
                var query2 = context.Speakers.Local.SingleOrDefault(s => s.Email == "*****@*****.**");
                Assert.IsNotNull(query2);

            }
        }
        public void CRUD()
        {
            // Note: The reason for the seperate using blocks is to simulate seperate calls to database.

            int speakerID;

            // Create new speaker
            using(var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var speaker = new Speaker
                                  {
                                      FirstName = "Test",
                                      LastName = "Speaker"
                                  };

                context.Speakers.Add(speaker);
                context.SaveChanges();
                speakerID = speaker.Id;

                Assert.IsTrue(speakerID != 0);
            }

            // Read and Update speaker
            using(var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var query = from s in context.Speakers
                            where s.Id == speakerID
                            select s;

                var speaker = query.FirstOrDefault();

                Assert.IsNotNull(speaker);

                speaker.Email = "*****@*****.**";
                context.SaveChanges();

                var checkSpeaker = query.FirstOrDefault();

                Assert.AreEqual(speaker.Email, checkSpeaker.Email);
            }

            // Delete: with out pulling record from database first
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var speaker = new Speaker {Id = speakerID};
                context.Speakers.Attach(speaker);
                context.Speakers.Remove(speaker);

                // can also be expressed as a single statement
                // context.Entry(speaker).State = EntityState.Deleted;

                context.SaveChanges();

                var query = from s in context.Speakers
                            where s.Id == speakerID
                            select s;

                var checkSpeaker = query.FirstOrDefault();

                Assert.IsNull(checkSpeaker);
            }
        }