Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var db = new PhonebookContext();

            Database.SetInitializer(new PhonebookMigrationStrategy());

            var contactsQuery =
                db.Contacts.Select(
                    c => new
            {
                Name   = c.Name,
                Phones = c.Phones.Select(p => new { p.PhoneNumber }),
                Emails = c.Emails.Select(e => new { e.EmailAddress })
            });

            foreach (var contact in contactsQuery)
            {
                Console.WriteLine("Contact: " + contact.Name);
                if (contact.Phones != null)
                {
                    foreach (var phone in contact.Phones)
                    {
                        Console.WriteLine("\tPhone: " + phone.PhoneNumber);
                    }
                }

                if (contact.Emails != null)
                {
                    foreach (var email in contact.Emails)
                    {
                        Console.WriteLine("\tEmail: " + email.EmailAddress);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static async Task SeedAsync(PhonebookContext context, int?retry = 0, bool useInMemory = true)
        {
            int retryForAvailability = retry.Value;

            try
            {
                // TODO: Only run this if using a real database
                if (!useInMemory)
                {
                    context.Database.Migrate();
                    context.Database.EnsureCreated();
                }

                if (!context.Phonebooks.Any())
                {
                    context.Phonebooks.Add(GetPreconfiguredPhonebook());
                    await context.SaveChangesAsync();
                }

                if (!context.Entries.Any())
                {
                    context.Entries.AddRange(GetPreconfiguredEntries());
                    await context.SaveChangesAsync();
                }
            }
            catch (Exception exception)
            {
                if (retryForAvailability < 10)
                {
                    retryForAvailability++;
                    await SeedAsync(context, retryForAvailability);
                }
                throw;
            }
        }
        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);
            }
        }
        static void Main()
        {
            using (var context = new PhonebookContext())
            {
                var contacts = context.Contacts
                    .Select(c => new
                    {
                        ContactName = c.Name,
                        ContactEmails = c.Emails.Select(e => e.EmailAddress),
                        ContactPhones = c.Phones.Select(p => p.PhoneNumber),
                    })
                    .ToList();

                foreach (var contact in contacts)
                {
                    Console.WriteLine(contact.ContactName);
                    Console.WriteLine("Emails: " + string.Join(", ", contact.ContactEmails));
                    Console.WriteLine("Phones: " + string.Join(", ", contact.ContactPhones));
                    Console.WriteLine();
                }
            }
        }
Ejemplo n.º 5
0
 public PhonebookRepository(PhonebookContext ctx)
 {
     this._ctx = ctx;
 }
Ejemplo n.º 6
0
 public PhonebookSeeder(PhonebookContext phonebookContext, IHostingEnvironment hosting)
 {
     this._ctx     = phonebookContext;
     this._hosting = hosting;
 }