public void AddUser(string userName)
 {
     using (var db = new PhoneBookContext())
     {
         var user = new Account(userName) { Phones = new List<Phone>() };
         db.Account.Add(user);
         db.SaveChanges();
     }
 }
 public void AddPhone(string userName, Phone phone)
 {
     using (var db = new PhoneBookContext())
     {
         var account = db.Account.Include(p => p.Phones).FirstOrDefault(user => user.UserName == userName);
         db.Account.Attach(account);
         account?.Phones.Add(phone);
         db.SaveChanges();
     }
 }
 public void AddPhone(string userName, IList<Phone> phones)
 {
     using (var db = new PhoneBookContext())
     {
         var account = db.Account.Include(p => p.Phones).FirstOrDefault(user => user.UserName == userName);
         db.Account.Attach(account);
         var entry = db.Entry(account);
         account?.Phones.AddRange(phones);
         entry.Property(e => e.Phones).IsModified = true;
         db.SaveChanges();
     }
 }
 public void AddPhone(int id, Phone phone)
 {
     using (var db = new PhoneBookContext())
     {
         var account = db.Account.Include(p => p.Phones).FirstOrDefault(user=>user.Id ==id);
         db.Account.Attach(account);
         var entry = db.Entry(account);
         account?.Phones.Add(phone);
         entry.Property(e => e.Phones).IsModified = true;
         db.SaveChanges();
     }
 }
 public void RemoveUser(string userName)
 {
     using (var db = new PhoneBookContext())
     {
         var account = db.Account.Include(p => p.Phones).FirstOrDefault(user => user.UserName == userName);
         db.Account.Remove(account);
         db.SaveChanges();
     }
 }
 public void RemovePhone(string userName, Phone phone)
 {
     using (var db = new PhoneBookContext())
     {
         var account = db.Account.Include(p=>p.Phones).FirstOrDefault(item => item.UserName == userName);
         if (account != null)
         {
             var phonetmp = account.Phones.FirstOrDefault(x => x.Number == phone.Number);
             db.Phones.Remove(phonetmp);
         }
         // db.Account.Attach(account);
        // var entry = db.Entry(account);
       //  account?.Phones.Remove(phone);
       //  entry.Property(e => e.Phones).IsModified = true;
         db.SaveChanges();
     }
 }