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.ToList().ForEach(context.DeleteObject); randomContact.Addresses.ToList().ForEach(context.DeleteObject); randomContact.PhoneNumbers.ToList().ForEach(context.DeleteObject); context.Contacts.DeleteObject(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 EntityCollection<EmailAddress>(), Addresses = new EntityCollection<Address>(), PhoneNumbers = 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.AddToContacts(newContact); context.SaveChanges(); }
private static void Cleanup(ContactsContext context) { var contactList = context.Contacts.ToList(); foreach (var contact in contactList) { // delete emails var emails = contact.EmailAddresses.ToList(); emails.ForEach(context.DeleteObject); // delete phone numbers var phoneNumbers = contact.PhoneNumbers.ToList(); phoneNumbers.ForEach(context.DeleteObject); // delete address var addresses = contact.Addresses.ToList(); addresses.ForEach(context.DeleteObject); // delete contact context.DeleteObject(contact); } 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()) { PhoneNumber phoneNumber = contact.PhoneNumbers.First(); context.DeleteObject(phoneNumber); } if (contact.EmailAddresses.Any()) { EmailAddress emailAddress = contact.EmailAddresses.First(); context.DeleteObject(emailAddress); } 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!"); } }
static void Main(string[] args) { Console.WriteLine("Back to Basics: Entity Framework"); Console.WriteLine("Model 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(); }