private static void ImportContactToDatabase(ContactDTO contactDTO) { if (contactDTO.Name == null) { throw new ArgumentException("Name is required"); } var newContact = new Contact() { Name = contactDTO.Name, Company = contactDTO.Company, Position = contactDTO.Position, Site = contactDTO.Site, Notes = contactDTO.Notes }; if (contactDTO.Emails != null) { newContact.Emails = contactDTO.Emails.Select(e => new Email() { EmailAddress = e }).ToList(); } if (contactDTO.Phones != null) { newContact.Phones = contactDTO.Phones.Select(p => new Phone() { PhoneNumber = p }).ToList(); } var context = new PhonebookContext(); context.Contacts.Add(newContact); context.SaveChanges(); }
static void Main() { var context = new PhonebookContext(); var contacts = context.Contacts .Include(c => c.Phones) .Include(c => c.Emails) .ToList(); foreach (var contact in contacts) { Console.WriteLine("Name: {0}", contact.Name); Console.WriteLine(" Position: {0}", contact.Position); Console.WriteLine(" Company: {0}", contact.Company); Console.WriteLine(" Emails: {0}", string.Join(", ", contact.Emails.Select(e => e.EmailAddress))); Console.WriteLine(" Phones: {0}", string.Join(", ", contact.Phones.Select(e => e.PhoneNumber))); Console.WriteLine(" Url: {0}", contact.Site); Console.WriteLine(" Notes: {0}", contact.Notes); Console.WriteLine(); } }