private static void ProcessMessage(PhonebookEntities context, JsonMessage messege) { if (messege.Content == null) { throw new ArgumentException("Content is required!"); } if (messege.DateTime == null) { throw new ArgumentException("DateTime is required!"); } if (messege.Recipient == null) { throw new ArgumentException("Recipient is required!"); } if (messege.Sender == null) { throw new ArgumentException("Sender is required!"); } var userMessage = new UserMessage() { Content = messege.Content, DateTime = (DateTime)messege.DateTime, Sender = context.Users.FirstOrDefault(u => u.Username == messege.Sender), Recipiant = context.Users.FirstOrDefault(u => u.Username == messege.Recipient), }; context.UserMessages.AddOrUpdate(userMessage); context.SaveChanges(); Console.WriteLine("Message \"{0}\" Imported", userMessage.Content); }
private static void AddContact(JToken contact, PhonebookEntities db) { if (contact["name"] == null) { throw new ArgumentException("Name is required"); } var newContact = new Contact { Name = contact["name"].ToString() }; if (contact["company"] != null) { newContact.Company = contact["company"].ToString(); } if (contact["position"] != null) { newContact.Position = contact["position"].ToString(); } if (contact["site"] != null) { newContact.Site = contact["site"].ToString(); } if (contact["notes"] != null) { newContact.Notes = contact["notes"].ToString(); } if (contact["phones"] != null) { foreach (var phone in contact["phones"]) { newContact.Phones.Add(new Phone { PhoneNumber = phone.ToString() }); } } if (contact["emails"] != null) { foreach (var email in contact["emails"]) { newContact.Emails.Add(new Email { EmailAddress = email.ToString() }); } } db.Contacts.Add(newContact); db.SaveChanges(); }
public static void Main() { using (var db = new PhonebookEntities()) { foreach (var contact in db.Contacts.Include(c => c.Emails).Include(c => c.Phones)) { Console.WriteLine("Name: " + contact.Name); Console.WriteLine("Emails: " + string.Join(", ", contact.Emails.Select(e => e.EmailAddress))); Console.WriteLine("Phones: " + string.Join(", ", contact.Phones.Select(p => p.PhoneNumber))); Console.WriteLine(); } } }
public static void Main() { using (var db = new PhonebookEntities()) { var contacts = JArray.Parse(File.ReadAllText("../../../Helper.Files/contacts.json")); foreach (var contact in contacts) { try { AddContact(contact, db); Console.WriteLine("Contact {0} imported", contact["name"]); } catch (ArgumentException e) { Console.WriteLine("Error: " + e.Message); } } } }
public static void Main(string[] args) { var context = new PhonebookEntities(); var serializer = new JavaScriptSerializer(); var json = File.ReadAllText("../../../messages.json"); var messeges = serializer.Deserialize <JsonMessage[]>(json); foreach (var messege in messeges) { try { ProcessMessage(context, messege); } catch (ArgumentException ex) { Console.Error.WriteLine("Error: {0}", ex.Message); } } }
public UnitOfWork() { _dbContext = new PhonebookEntities(); }