protected void ButtonSendMessage_Click(object sender, EventArgs e) { string username = this.TextBoxUsername.Text; string message = this.TextBoxMessage.Text; var context = new WebChatContext(); var author = context.Users.FirstOrDefault(u => u.Username == username); if (author == null) { var newUser = new User() { Username = username }; author = context.Users.Add(newUser); context.SaveChanges(); } var newMessage = new Message() { Text = this.TextBoxMessage.Text, Author = author, CreationDate = DateTime.Now }; context.Messages.Add(newMessage); context.SaveChanges(); }
protected void Page_Load(object sender, EventArgs e) { // Creating the database at the first run // Comment after first run var context = new WebChatContext(); context.Messages.ToList(); }
public IQueryable<Message> ListViewMessages_GetData() { var context = new WebChatContext(); return context.Messages .Include(m => m.Author) .OrderBy(m => m.CreationDate); }