private static void ImportMessageToDB(MessageDTO message) { if (string.IsNullOrWhiteSpace(message.Content)) { throw new ArithmeticException("Content is required"); } if (string.IsNullOrWhiteSpace(message.Recipient)) { throw new ArithmeticException("Recipient is required"); } if (string.IsNullOrWhiteSpace(message.Sender)) { throw new ArithmeticException("Sender is required"); } var context = new ChatSystemContext(); var recipientId = context.Users .Where(u => u.Username == message.Recipient) .Select(r => r.Id) .FirstOrDefault(); var senderId = context.Users .Where(u => u.Username == message.Sender) .Select(s => s.Id) .FirstOrDefault(); var userMessage = new UserMessage() { Content = message.Content, DateTime = message.DateTime, RecipientId = recipientId, SenderId = senderId }; context.UserMessages.Add(userMessage); context.SaveChanges(); }
static void Main() { var context = new ChatSystemContext(); // Console.WriteLine(context.Users.Count()); var channels = context.Channels .Select(c => new { ChannelName = c.Name, ChannelMessages = c.ChannelMessages }); foreach (var ch in channels) { Console.WriteLine(ch.ChannelName); Console.WriteLine("-- Messages: --"); foreach (var chMessgae in ch.ChannelMessages) { Console.WriteLine("Content: {0}, DateTime: {1}, User: {2}", chMessgae.Content, chMessgae.DateTime, chMessgae.User.Username); } Console.WriteLine(); } }