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(); }