コード例 #1
0
        static void Main()
        {
            var context = new PhonebookContext();

            var contacts = context.Contacts
                .Select(c => new
                {
                    c.Name,
                    c.Note,
                    c.Position,
                    c.Url,
                    c.Company,
                    Emails = c.Emails.Select(e => e.EmailAdress),
                    Phones = c.Phones.Select(p => p.PhoneNumber)
                });

            foreach (var contact in contacts)
            {
                Console.WriteLine("Name: {0}", contact.Name);
                if (contact.Position != null)
                {
                    Console.WriteLine("  Position: {0}", contact.Position);
                }

                if (contact.Company != null)
                {
                    Console.WriteLine("  Company: {0}", contact.Company);
                }

                if (contact.Emails.Count() != 0)
                {
                    string emails = String.Join(", ", contact.Emails.ToList());
                    Console.WriteLine("  Emails: {0}", emails);
                }

                if (contact.Phones.Count() != 0)
                {
                    string phones = String.Join(", ", contact.Phones.ToList());
                    Console.WriteLine("  Phones: {0}", phones);
                }

                if (contact.Url != null)
                {
                    Console.WriteLine("  URL: {0}", contact.Url);
                }

                if (contact.Note != null)
                {
                    Console.WriteLine("  Notes: {0}", contact.Note);
                }
            }
        }
コード例 #2
0
        static void Main()
        {
            var context = new PhonebookContext();

            context.Channels.Count();
        }
        static void Main()
        {
            var context = new PhonebookContext();

            JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
            var contacts = jsSerializer.Deserialize<List<Contact>>(File.ReadAllText(@"../../contacts.json"));

            foreach (var contact in contacts)
            {
                if (String.IsNullOrEmpty(contact.Name))
                {
                    Console.WriteLine("Error: Name is required");
                    continue;
                }

                _6.EFCodeFirstPhonebook.Contact newContact = new _6.EFCodeFirstPhonebook.Contact();
                newContact.Name = contact.Name;
                if (!String.IsNullOrEmpty(contact.Position))
                {
                    newContact.Position = contact.Position;
                }

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

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

                if (!String.IsNullOrEmpty(contact.Notes))
                {
                    newContact.Note = contact.Notes;
                }

                if (contact.Emails != null)
                {
                    foreach (var email in contact.Emails)
                    {
                        newContact.Emails.Add(new Email()
                        {
                            EmailAdress = email
                        });
                    }
                }

                if (contact.Phones != null)
                {
                    foreach (var phone in contact.Phones)
                    {
                        newContact.Phones.Add(new Phone()
                        {
                            PhoneNumber = phone
                        });
                    }
                }

                Console.WriteLine("Contact {0} imported", contact.Name);
                context.Contacts.Add(newContact);
                context.SaveChanges();
            }
        }