public void CreateNewContact(Contact contactToCreate)
        {
            if (ExceptionToThrow != null)
                throw ExceptionToThrow;

            _db.Add(contactToCreate);
        }
        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();
        }
        static void Main()
        {
            var context = new PhonebookContext();

            string contactsJson = File.ReadAllText("../../contacts.json");

            var contacts = JArray.Parse(contactsJson);
            foreach (JToken contact in contacts)
            {
                Contact dbContact = new Contact();

                if (contact["name"] == null)
                {
                    Console.WriteLine("Error: name is required");
                    continue;
                }

                dbContact.Name = contact["name"].ToString();

                if (contact["phones"] != null)
                {
                    foreach (var phone in contact["phones"])
                    {
                        dbContact.Phones.Add(new Phone(){PhoneNumber = phone.ToString()});
                    }
                }
                if (contact["emails"] != null)
                {
                    foreach (var email in contact["emails"])
                    {
                        dbContact.Emails.Add(new Email(){EmailAddress = email.ToString()});
                    }
                }

                if (contact["position"] != null)
                {
                    dbContact.Position = contact["position"].ToString();
                }
                if (contact["company"] != null)
                {
                    dbContact.Company = contact["company"].ToString();
                }
                if (contact["url"] != null)
                {
                    dbContact.Url = contact["url"].ToString();
                }
                if (contact["notes"] != null)
                {
                    dbContact.Notes = contact["notes"].ToString();
                }

                context.Contacts.Add(dbContact);
                context.SaveChanges();
                Console.WriteLine("Contact {0} imported", dbContact.Name);
            }
        }
 public void SaveChanges(Contact contactToUpdate)
 {
     foreach (Contact contact in _db) {
         if (contact.Id == contactToUpdate.Id) {
             _db.Remove(contact);
             _db.Add(contactToUpdate);
             break;
         }
     }
 }
        public ActionResult Create(Contact contact)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.CreateNewContact(contact);
                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex);
                ViewData["CreateError"] = "Unable to create!";
            }

            return View("Create");
        }
 public void CreateNewContact(Contact contactToCreate)
 {
     _db.AddToContacts(contactToCreate);
     _db.SaveChanges();
 }
 public void Add(Contact contactToAdd)
 {
     _db.Add(contactToAdd);
 }