static void Main() { //Create an Entity Framework (EF) code first data model for keeping phonebook holding contacts with phones and emails. //It should have several entities: //• Contacts have name and optionally position, company, emails, phones, site (URL) and notes (free text). //• Emails hold email address. //• Phones hold phone number. //Seed your database with a few contacts, using the EF migrations framework. //It is OK to drop the database in case of model changes or use any other migration strategy //like automatic upgrade to the latest DB schema. //To test your data model, list all contacts along with their phones and emails. var context = new PhonebookContext(); //set database //var count = context.Contacts.Count(); //Console.WriteLine(count); //list all contacts along with their phones and emails var people = context.Contacts .Select(c => new { ContactName = c.Name, ContactPhones = c.Phones.Select(p => p.PhoneNumber), ContactEmails = c.Emails.Select(e => e.EmailAddress) }).ToList(); foreach (var person in people) { Console.WriteLine("--" + person.ContactName); Console.WriteLine("Phones: " + String.Join(", ", person.ContactPhones)); Console.WriteLine("Emails: " + String.Join(", ", person.ContactEmails)); } }
static void Main() { //Problem 7. Import Contacts from JSON //Write a C# application based on your EF code first data model for importing into the DB //a set of phonebook contacts given in the file contacts.json. //The only mandatory property is name. All the others are optional. //You should parse the JSON and throw an exception in case of incorrect data, e.g. when a required element is missing //or an invalid value is given. The size of the JSON file will be less than 10 MB. Use a JSON parser by choice. //You should correctly import the contacts into the DB. //You should correctly import the emails and phones into the DB. //Insert each contact in a separate transaction. A contact should either be inserted correctly along with all its emails //and phones, or no part of it should be inserted at all. //Print as output a single line for each contact from the input JSON: either "Contact <name> imported" //or "Error: <error message>". Error messages should describe briefly the problem (as free text) and may optionally //include exception stack-trace. var context = new PhonebookContext(); //JsonConvert.DeserializeObject() не поддържа десериализация на масиви от обекти string objectsArray = File.ReadAllText("../../contacts.json"); JArray contacts = JArray.Parse(objectsArray); foreach (JToken contact in contacts) //JToken представлява един ред от JSON-a и мога да си го викам по име { Contact dbContact = new Contact(); //var name = contact["name"] ?? null; if (contact["name"] == null) { Console.WriteLine("Error: Name is required"); continue; } dbContact.Name = contact["name"].ToString(); if (contact["company"] != null) { dbContact.Company = contact["company"].ToString(); } if (contact["position"] != null) { dbContact.Position = contact["position"].ToString(); } if (contact["site"] != null) { dbContact.SiteUrl = contact["site"].ToString(); } if (contact["notes"] != null) { dbContact.Notes = contact["notes"].ToString(); } if (contact["phones"] != null) { foreach (var phone in contact["phones"]) { Phone dbPhone = new Phone() { PhoneNumber = phone.ToString() }; dbContact.Phones.Add(dbPhone); } } if (contact["emails"] != null) { foreach (var email in contact["emails"]) { Email dbEmail = new Email() { EmailAddress = email.ToString() }; dbContact.Emails.Add(dbEmail); } } context.Contacts.Add(dbContact); Console.WriteLine("Contact {0} imported", dbContact.Name); context.SaveChanges(); } }