Пример #1
0
 public void AddPerson(Person person)
 {
     using (var context = new PhoneBookContext()){
         context.Phonebook.Add(person);
         context.SaveChanges();
     }
 }
Пример #2
0
 public Person FindPerson(string x)
 {
     using (var context = new PhoneBookContext()){
         Person row = context.Phonebook.FirstOrDefault(p => p.Name == x);
         return(row);
     }
 }
Пример #3
0
        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");
        }
Пример #4
0
        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);
            }
        }
Пример #5
0
 public Person[] FindAll()
 {
     using (var db = new PhoneBookContext()){
         return(db.Phonebook.ToArray());
     }
 }
Пример #6
0
 public static void initializeDatabase(PhoneBookContext context)
 {
     context.Database.Migrate();
 }