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 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(); }
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 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(); } }