public void Working_With_Current_Orginal_DatabaseValues()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var speaker = context.Speakers.Find(1);
                var entity = context.Entry(speaker);

                // create a second DbContext to simulate change from another user.
                using (var context2 = new CodeCampContext(TestHelpers.TestDatabaseName))
                {
                    var changeSpeaker = context2.Speakers.Find(1);
                    changeSpeaker.Bio = "Bio has changed while you were out...";
                    context2.SaveChanges();
                }

                Console.WriteLine("Current Values before edit");
                TestHelpers.WritePropertyValues(entity.CurrentValues);

                speaker.Email = "*****@*****.**";
                speaker.TwitterAlias = "@steve";

                Console.WriteLine("Current Values after edit");
                TestHelpers.WritePropertyValues(entity.CurrentValues);

                Console.WriteLine("Orginal Values");
                TestHelpers.WritePropertyValues(entity.OriginalValues);

                Console.WriteLine("Database Values (trip to database)");
                TestHelpers.WritePropertyValues(entity.GetDatabaseValues());
            }
        }
        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);
            }
        }
        public void CRUD_With_Relationships()
        {
            // Add a new presentation through a Speaker's presentation collection.
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                Presentation presentation = new Presentation
                                                {
                                                    Title = "Another presentation about .NET"
                                                };

                Speaker speaker = context.Speakers.FirstOrDefault(s => s.LastName == "Pugh");

                speaker.Presentations.Add(presentation);

                context.SaveChanges();

                var query = from p in context.Presentations
                            where p.Speaker.LastName == "Pugh"
                            select p;

                Assert.AreEqual(2, query.Count());
            }

            // Add a speaker to a new presentation.
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var query = from s in context.Speakers
                            where s.Id == 1
                            select s;

                Speaker speaker = query.FirstOrDefault();

                Presentation presentation = new Presentation
                                                {
                                                    Title = "More about REST",
                                                    Speaker = speaker
                                                };

                context.Presentations.Add(presentation);
                context.SaveChanges();

                // Requery Speaker, this time get presentations.
                speaker = query.Include(s => s.Presentations).FirstOrDefault();

                Assert.AreEqual(2, speaker.Presentations.Count);

                Console.WriteLine("New Presentation added to existing Speaker");
                foreach (var presentation1 in context.Presentations.Local)
                {
                    Console.WriteLine("Speaker: {0}, Presentation: {1}", presentation1.Speaker.FirstName, presentation1.Title);
                }
                Console.WriteLine();

                // Change speaker of presentation
                var speaker2 = context.Speakers.Include(s => s.Presentations).FirstOrDefault(s => s.Id == 8);
                presentation.Speaker = speaker2;

                context.SaveChanges();

                Console.WriteLine("Presenation moved to a different speaker");
                foreach (var presentation1 in context.Presentations.Local.OrderBy(p=>p.Speaker.LastName))
                {
                    Console.WriteLine("Speaker: {0}, Presentation: {1}", presentation1.Speaker.FirstName, presentation1.Title);
                }
                Console.WriteLine();

                // Remove Presentation by setting Speaker to null in Presentation record.
                presentation.Speaker = null; // This only works because Speaker is not required. If it was required an exception would be thrown on save.

                // Remove Presentation by removing it from the Presentation navigation property in Speaker;
                Presentation presentation2 = speaker2.Presentations.FirstOrDefault();
                speaker2.Presentations.Remove(presentation2);

                context.SaveChanges();

                Console.WriteLine("Presenation removed. No longer assigned to any speaker.");
                foreach (var presentation1 in context.Presentations.Local)
                {
                    Console.WriteLine("Speaker: {0}, Presentation: {1}",
                        presentation1.Speaker != null ? presentation1.Speaker.FirstName : "No Speaker Assigned",
                        presentation1.Title);
                }
            }
        }
        public void ComlexTypes_With_Current_Orginal_DatabaseValues()
        {
            using (var context = new CodeCampContext(TestHelpers.TestDatabaseName))
            {
                var codeCamp = context.CodeCampEvents.FirstOrDefault();
                var entity = context.Entry(codeCamp);

                // create a second DbContext to simulate change from another user.
                using (var context2 = new CodeCampContext(TestHelpers.TestDatabaseName))
                {
                    var codeCamp2 = context2.CodeCampEvents.FirstOrDefault();
                    codeCamp2.Location.StreetAddress2 = "Street 2 Changed";
                    context2.SaveChanges();
                }

                Console.WriteLine("Current Values before edit");
                TestHelpers.WritePropertyValues(entity.CurrentValues);

                codeCamp.Location.StreetAddress2 = "Suite 101";

                Console.WriteLine("Current Values after edit");
                TestHelpers.WritePropertyValues(entity.CurrentValues);

                Console.WriteLine("Orginal Values");
                TestHelpers.WritePropertyValues(entity.OriginalValues);

                Console.WriteLine("Database Values (trip to database)");
                TestHelpers.WritePropertyValues(entity.GetDatabaseValues());
            }
        }