static void Main() { var phonebookContext = new PhonebookContext(); string jsonContacts = File.ReadAllText("../../contacts.json"); var contacts = JsonConvert.DeserializeObject <ContactDTO[]>(jsonContacts); foreach (var contactDTO in contacts) { try { if (contactDTO.Name == null) { throw new ArgumentException("Name is requeired."); } var contact = new Contact() { Name = contactDTO.Name, Position = contactDTO.Position, Company = contactDTO.Company, Site = contactDTO.Site, Notes = contactDTO.Notes }; if (contactDTO.Emails != null) { contact.Emails = contactDTO.Emails.Select(e => new Email() { EmailAddress = e }).ToList(); } if (contactDTO.Phones != null) { contact.Phones = contactDTO.Phones.Select(pn => new Phone() { PhoneNumber = pn }).ToList(); } var context = new PhonebookContext(); context.Contacts.Add(contact); context.SaveChanges(); Console.WriteLine("Contact {0} imported", contactDTO.Name); } catch (ArgumentException exc) { Console.WriteLine("Error: {0}", exc.Message); } } }
public static void Main() { var context = new PhonebookContext(); var contacts = context.Contacts .Select(c => new { name = c.Name, position = c.Position, company = c.Company, site = c.Site, notes = c.Notes, phones = c.Phones.Select(p => p.PhoneNumber), emails = c.Emails.Select(e => e.EmailAddress) }); foreach (var contact in contacts) { string phones = null; if (contact.phones.Count() > 0) { phones = string.Join("; ", contact.phones); } string emails = null; if (contact.emails.Count() > 0) { emails = string.Join("; ", contact.emails); } Console.WriteLine("Name: {0}\r\nPosition: {1}\r\nCompany: {2}\r\nSite: {3}\r\nNotes: {4}\r\nPhones: {5}\r\nEmails: {6}\r\n", contact.name ?? "(no)", contact.position ?? "(no)", contact.company ?? "(no)", contact.site ?? "(no)", contact.notes ?? "(no)", phones ?? "(no)", emails ?? "(no)"); } }