private static void DeleteRandomContact(ContactsContext context)
        {
            Random random = new Random();

            var randomContact = context.Contacts.OrderBy(p => p.ContactId).Skip(random.Next(context.Contacts.Count())).First();

            randomContact.EmailAddresses.Clear();
            randomContact.Addresses.Clear();
            randomContact.PhoneNumbers.Clear();

            context.Contacts.Remove(randomContact);

            context.SaveChanges();
        }
        private static void InsertRandomContact(ContactsContext context)
        {
            // a little bit of helper stuff
            string[] firstNames = { "Kevin", "Rich", "Eve", "Leanne", "Chris" };
            string[] lastNames = { "Griffin", "Dudley", "Turzillo", "Lappe", "Bannon" };

            Random randomNumber = new Random();

            // create a new contact
            Contact newContact = new Contact()
            {
                FirstName = firstNames[randomNumber.Next(firstNames.Count())],
                LastName = lastNames[randomNumber.Next(lastNames.Count())],
                EmailAddresses = new List<EmailAddress>(new EntityCollection<EmailAddress>()),
                Addresses = new List<Address>(new EntityCollection<Address>()),
                PhoneNumbers = new List<PhoneNumber>(new EntityCollection<PhoneNumber>())
            };

            // add a couple email addresses
            newContact.EmailAddresses.Add(new EmailAddress()
            {
                Email =
                    string.Format("{0}.{1}@gmail.com", newContact.FirstName,
                                  newContact.LastName)
            });
            newContact.EmailAddresses.Add(new EmailAddress()
            {
                Email =
                    string.Format("{0}.{1}@yahoo.com", newContact.FirstName,
                                  newContact.LastName)
            });

            // add a couple phone numbers
            for (int x = 0; x < randomNumber.Next(1, 5); x++)
            {
                newContact.PhoneNumbers.Add(new PhoneNumber()
                {
                    Number =
                        string.Format("555-{0:000}-{1:0000}", randomNumber.Next(0, 999),
                                      randomNumber.Next(0, 9999))
                });
            }

            // add contact
            context.Contacts.Add(newContact);
            context.SaveChanges();
        }
        private static void Cleanup(ContactsContext context)
        {
            var contactList = context.Contacts.ToList();
            foreach (var contact in contactList)
            {
                // delete emails
                contact.EmailAddresses.Clear();

                // delete phone numbers
                contact.PhoneNumbers.Clear();

                // delete address
               contact.Addresses.Clear();

                // delete contact
                context.Contacts.Remove(contact);

            }
            context.SaveChanges();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Back to Basics: Entity Framework");
            Console.WriteLine("Code First Demo");
            Console.WriteLine();

            /*
             * Load an instance of the context
             */

            var context = new ContactsContext();
            Cleanup(context);

            /*
             * Select all the contacts
             */

            SelectAllContacts(context);
            Console.WriteLine();

            /*
             * Insert a new contact
             */

            for (int x = 0; x < 10; x++)
                InsertRandomContact(context);
            Console.WriteLine();

            /*
             * Select all the contacts AGAIN
             */

            SelectAllContacts(context);
            Console.WriteLine();

            /*
             * Update a (random) contact
             */

            UpdateContacts(context);
            Console.WriteLine();

            /*
             * Select all the contacts A THIRD TIME
             */

            SelectAllContacts(context);
            Console.WriteLine();

            /*
             * Delete a contact
             */
            DeleteRandomContact(context);
            Console.WriteLine();

            /*
             * Select all the contacts ONE LAST TIME!
             */

            SelectAllContacts(context);
            Console.WriteLine();
        }
        private static void UpdateContacts(ContactsContext context)
        {
            var contacts = context.Contacts.ToList();
            foreach (var contact in contacts)
            {
                // we're going to swap all the first names with last names
                var firstName = contact.FirstName;
                var lastName = contact.LastName;

                contact.FirstName = lastName;
                contact.LastName = firstName;

                // we're now going to delete a phone number and email address
                if (contact.PhoneNumbers.Any())
                    contact.PhoneNumbers.Remove(contact.PhoneNumbers.First());

                if (contact.EmailAddresses.Any())
                    contact.EmailAddresses.Remove(contact.EmailAddresses.First());

                context.SaveChanges();
            }
        }
 private static void SelectAllContacts(ContactsContext context)
 {
     if (context.Contacts.Any())
     {
         foreach (var contact in context.Contacts)
         {
             Console.WriteLine(
                 string.Format("I see {0} {1}.  They have {2} phone numbers and {3} email addresses.",
                               contact.FirstName, contact.LastName, contact.PhoneNumbers.Count(),
                               contact.EmailAddresses.Count()));
         }
     }
     else
     {
         Console.WriteLine("No contacts where found!");
     }
 }