private static void InsertNewContact(RootObject newContact, PhonebookEntities context)
        {
            Contact contact = new Contact();

            if (newContact.Name == null)
            {
                throw new ArgumentException("Error: Name is required");
            }

            contact.Name = newContact.Name;

            if (newContact.Company != null)
            {
                contact.Company = newContact.Company;
            }

            if (newContact.Notes != null)
            {
                contact.Notes = newContact.Notes;
            }

            if (newContact.Position != null)
            {
                contact.Position = newContact.Position;
            }

            if (newContact.Site != null)
            {
                contact.Url = newContact.Site;
            }

            if (newContact.Emails != null && newContact.Emails.Count > 0)
            {
                foreach (var email in newContact.Emails)
                {
                    if (!email.Contains("@"))
                    {
                        throw new ArgumentException("Not valid email!");
                    }

                    Email newEmail = new Email();
                    newEmail.EmailAddress = email;
                    contact.Emails.Add(newEmail);
                }
            }

            if (newContact.Phones != null && newContact.Phones.Count > 0)
            {
                foreach (var phone in newContact.Phones)
                {
                    Phone newPhone = new Phone();
                    newPhone.Number = phone;
                    contact.Phones.Add(newPhone);
                }
            }

            context.Contacts.Add(contact);
            context.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(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 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();
     }
 }
        private static void ImportContact(JToken contact)
        {
            using (var context = new PhonebookEntities())
            {
                var newContact = new Contact();

                if (contact["name"] == null)
                {
                    throw new ArgumentException("Name is required.");
                }

                newContact.Name = contact["name"].Value<string>();

                if (contact["site"] != null)
                {
                    newContact.Url = contact["site"].Value<string>();
                }
                if (contact["position"] != null)
                {
                    newContact.Position = contact["position"].Value<string>();
                }
                if (contact["company"] != null)
                {
                    newContact.Company = contact["company"].Value<string>();
                }
                if (contact["notes"] != null)
                {
                    newContact.Notes = contact["notes"].Value<string>();
                }

                if (contact["emails"] != null)
                {
                    var emails = contact["emails"];

                    foreach (var email in emails)
                    {
                        var newEmail = new Email
                        {
                            EmailAdress = email.Value<string>()
                        };

                        newContact.Emails.Add(newEmail);
                    }
                }

                if (contact["phones"] != null)
                {
                    var phones = contact["phones"];

                    foreach (var phone in phones)
                    {
                        var newPhone = new Phone
                        {
                            Number = phone.Value<string>()
                        };

                        newContact.Phones.Add(newPhone);
                    }
                }

                context.Contacts.Add(newContact);
                context.SaveChanges();
            }
        }