コード例 #1
0
        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();
        }
コード例 #2
0
        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 }));
        }