public void AddPerson(Person person) { using (var context = new PhoneBookContext()){ context.Phonebook.Add(person); context.SaveChanges(); } }
public Person FindPerson(string x) { using (var context = new PhoneBookContext()){ Person row = context.Phonebook.FirstOrDefault(p => p.Name == x); return(row); } }
public static void CleanUp(PhoneBookContext context) { foreach (var person in context.Persons) { context.Persons.Remove(person); } context.SaveChanges(); context.Database.ExecuteSqlCommandAsync("EXEC sp_MSforeachtable 'DROP TABLE PERSONS"); }
static void Main(string[] args) { var context = new PhoneBookContext(); try { DatabaseUtil.initializeDatabase(context); /* TODO: create person objects and put them in the PhoneBook and database * John Smith, (248) 123-4567, 1234 Sand Hill Dr, Royal Oak, MI * Cynthia Smith, (824) 128-8758, 875 Main St, Ann Arbor, MI */ var John = new Person("John", "Smith", "(248) 123 - 4567", "1234 Sand Hill Dr", "Royal Oak", "MI"); var Cynthia = new Person("Cynthia", "Smith", "(824) 128 - 8758", "875 Main St", "Ann Arbor", "MI"); var phoneBook = new PhoneBook(context); phoneBook.AddPerson(John); phoneBook.AddPerson(Cynthia); // TODO: print the phone book out to System.out new PhoneBookPrinter().Print(phoneBook); // TODO: find Cynthia Smith and print out just her entry var cynthiaRecord = phoneBook.Find(m => m.FirstName == "Cynthia" && m.LastName == "Smith") .FirstOrDefault(); Console.WriteLine(); Console.WriteLine(cynthiaRecord?.ToString()); // TODO: insert the new person objects into the database Console.ReadLine(); DatabaseUtil.CleanUp(context); } finally { DatabaseUtil.CleanUp(context); } }
public Person[] FindAll() { using (var db = new PhoneBookContext()){ return(db.Phonebook.ToArray()); } }
public static void initializeDatabase(PhoneBookContext context) { context.Database.Migrate(); }