public void AddUserToRoom(User user, ChatRoom room) { //mapping many to many UserChatRooms mapper = new UserChatRooms(); mapper.User = user; mapper.ChatRoom = room; //--- room.UserChatRooms.Add(mapper); Db.SaveChanges(); }
public IActionResult CreateRoom() { var user = db.Users.Include(x => x.UserChatRooms) .ThenInclude(x => x.ChatRoom) .FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name); //mapping many to many UserChatRooms mapper = new UserChatRooms() { User = user, ChatRoom = new ChatRoom() }; mapper.ChatRoom.Owner = user.UserName; user.UserChatRooms.Add(mapper); db.SaveChanges(); return(RedirectToAction("RoomChat", new { Id = mapper.ChatRoomId })); }