private void TestEvents(Func <Context, Int32> saveChangesAction)
        {
            insertingFiredCount    = 0;
            updatingFiredCount     = 0;
            deletingFiredCount     = 0;
            insertFailedFiredCount = 0;
            updateFailedFiredCount = 0;
            deleteFailedFiredCount = 0;
            insertedFiredCount     = 0;
            updatedFiredCount      = 0;
            deletedFiredCount      = 0;
            updateFailedThingValue = null;
            using (var context = new Context()) {
                var people      = context.People.ToList();
                var nickStrupat = new Person {
                    FirstName = "Nick",
                    LastName  = "Strupat",
                };
                AddHandlers(nickStrupat);
                nickStrupat.Triggers().Deleting += e => {
                    e.Entity.IsMarkedDeleted = true;
                    e.Cancel();
                };
                context.People.Add(nickStrupat);

                var johnSmith = new Person {
                    FirstName = "John",
                    LastName  = "Smith"
                };
                AddHandlers(johnSmith);
                context.People.Add(johnSmith);
                AssertNoEventsHaveFired();

                saveChangesAction(context);
                Assert.IsTrue(nickStrupat.Number == 42);
                AssertInsertEventsHaveFired();
                Assert.IsTrue(context.Things.First().Value == "Insert trigger fired for Nick");

                nickStrupat.FirstName = "Nicholas";
                saveChangesAction(context);
                AssertUpdateEventsHaveFired();

                nickStrupat.LastName = null;
                try {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex) {
                    nickStrupat.LastName = "Strupat";
                }
                catch (Exception ex) {
                    Assert.Fail(ex.GetType().Name + " exception caught");
                }
                context.SaveChanges();
                Assert.AreEqual(updateFailedFiredCount, 1);
                Assert.IsTrue(context.Things.OrderByDescending(x => x.Id).First().Value == updateFailedThingValue);

                context.People.Remove(nickStrupat);
                context.People.Remove(johnSmith);
                saveChangesAction(context);
                AssertAllEventsHaveFired();

                context.Database.Delete();
            }
        }
        private void TestEvents(Func<Context, Int32> saveChangesAction) {
            insertingFiredCount = 0;
            updatingFiredCount = 0;
            deletingFiredCount = 0;
			insertFailedFiredCount = 0;
			updateFailedFiredCount = 0;
			deleteFailedFiredCount = 0;
            insertedFiredCount = 0;
            updatedFiredCount = 0;
			deletedFiredCount = 0;
			updateFailedThingValue = null;
            using (var context = new Context()) {
                var nickStrupat = new Person {
                                                 FirstName = "Nick",
                                                 LastName = "Strupat",
                                             };
                AddHandlers(nickStrupat);
				nickStrupat.Triggers().Deleting += e => {
					e.Entity.IsMarkedDeleted = true;
					e.Cancel();
				};
                context.People.Add(nickStrupat);

	            var johnSmith = new Person {
                                               FirstName = "John",
                                               LastName = "Smith"
                                           };
                AddHandlers(johnSmith);
                context.People.Add(johnSmith);
                AssertNoEventsHaveFired();

                saveChangesAction(context);
				Assert.IsTrue(nickStrupat.Number == 42);
                AssertInsertEventsHaveFired();
				Assert.IsTrue(context.Things.First().Value == "Insert trigger fired for Nick");

                nickStrupat.FirstName = "Nicholas";
                saveChangesAction(context);
				AssertUpdateEventsHaveFired();

				nickStrupat.LastName = null;
				try {
					context.SaveChanges();
				}
				catch (DbEntityValidationException ex) {
					nickStrupat.LastName = "Strupat";
				}
				catch (Exception ex) {
					Assert.Fail(ex.GetType().Name + " exception caught");
				}
				context.SaveChanges();
				Assert.AreEqual(updateFailedFiredCount, 1);
				Assert.IsTrue(context.Things.OrderByDescending(x => x.Id).First().Value == updateFailedThingValue);

                context.People.Remove(nickStrupat);
                context.People.Remove(johnSmith);
                saveChangesAction(context);
				AssertAllEventsHaveFired();

                context.Database.Delete();
            }
        }
        private static void CreateSmallTestSet(Context db)
        {
            db.Contacts.Add(new Contact
            {
                FirstName    = "FN1",
                LastName     = "LN1",
                Title        = "Director",
                Id           = Guid.NewGuid(),
                BirthDate    = DateTime.Today,
                PhoneNumbers = new List <PhoneNumber>()
                {
                    new PhoneNumber {
                        Id     = Guid.NewGuid(),
                        Number = "10134"
                    },
                    new PhoneNumber {
                        Id     = Guid.NewGuid(),
                        Number = "15678"
                    },
                }
            });
            db.Contacts.Add(new Contact
            {
                FirstName    = "FN2",
                LastName     = "LN2",
                Title        = "Associate",
                Id           = Guid.NewGuid(),
                BirthDate    = DateTime.Today,
                PhoneNumbers = new List <PhoneNumber>()
                {
                    new PhoneNumber {
                        Id     = Guid.NewGuid(),
                        Number = "20134"
                    },
                    new PhoneNumber {
                        Id     = Guid.NewGuid(),
                        Number = "25678"
                    },
                },
                Emails = new List <Email>()
                {
                    new Email {
                        Id = Guid.NewGuid(), Address = "*****@*****.**"
                    },
                    new Email {
                        Id = Guid.NewGuid(), Address = "*****@*****.**"
                    },
                }
            });
            db.Contacts.Add(new Contact
            {
                FirstName = "FN3",
                LastName  = "LN3",
                Title     = "Vice President",
                Id        = Guid.NewGuid(),
                BirthDate = DateTime.Today,
                Emails    = new List <Email>()
                {
                    new Email {
                        Id = Guid.NewGuid(), Address = "*****@*****.**"
                    },
                    new Email {
                        Id = Guid.NewGuid(), Address = "*****@*****.**"
                    },
                }
            });

            var blogPost1 = BlogPost.Create("BP1");

            blogPost1.Comments = new List <Comment>()
            {
                new Comment()
                {
                    Text = "C1"
                }
            };
            db.BlogPosts.Add(blogPost1);

            var blogPost2 = BlogPost.Create("BP2");

            blogPost2.Comments = new List <Comment>()
            {
                new Comment()
                {
                    Text = "C2"
                },
                new Comment()
                {
                    Text = "C3"
                }
            };
            db.BlogPosts.Add(blogPost2);

            var blogPost3 = BlogPost.Create("BP3");

            blogPost3.Comments = new List <Comment>()
            {
                new Comment()
                {
                    Text = "C4"
                },
                new Comment()
                {
                    Text = "C5"
                },
                new Comment()
                {
                    Text = "C6"
                }
            };
            db.BlogPosts.Add(blogPost3);

            db.SaveChanges();
        }