コード例 #1
0
        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();
        }
コード例 #2
0
        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();
            }
        }