private void TransferVideoComment() { var oldVideoComments = _oldDbContext.VideoComment.Include(mr => mr.Author).Include(mr => mr.Video).ToList(); var newVideoComments = _newDbContext.VideoComments.ToList(); foreach (var oldVideoComment in oldVideoComments) { var author = _newDbContext.AspNetUsers.FirstOrDefault(u => u.UserName == oldVideoComment.Author.Username); var video = _newDbContext.Videos.FirstOrDefault(m => m.Title == oldVideoComment.Video.Title); var findVideoComment = newVideoComments.FirstOrDefault(mr => oldVideoComment.Author.Username == author.UserName && oldVideoComment.Video.Title == video.Title); if (findVideoComment == null) { var newVideoComment = new VideoComments() { VideoId = video.Id, AuthorId = author.Id, Comment = oldVideoComment.Text, CreatedAt = oldVideoComment.CreatedAt, UpdatedAt = oldVideoComment.UpdatedAt }; _newDbContext.VideoComments.Add(newVideoComment); _newDbContext.SaveChanges(); } } }
public async Task <JsonNetResult> ByVideo(GetVideoCommentsViewModel model) { VideoComments result = await _comments.GetVideoComments(new GetVideoComments { VideoId = model.VideoId, PageSize = model.PageSize, FirstCommentIdOnPage = model.FirstCommentIdOnPage }); // For the ViewModel, we also want to include the information about a user who made the comments on the video, so // get the user profile information for the comments and then use a LINQ to Objects Join to merge the two together // (this should be OK since the dataset should be small) IEnumerable <UserProfile> userProfiles = await _userManagement.GetUserProfiles(result.Comments.Select(c => c.UserId).ToHashSet()); var returnModel = new VideoCommentsViewModel { VideoId = result.VideoId, Comments = result.Comments.Join(userProfiles, c => c.UserId, up => up.UserId, (c, up) => new VideoCommentViewModel { CommentId = c.CommentId, Comment = c.Comment, CommentTimestamp = c.CommentTimestamp, UserProfileUrl = Url.Action("Info", "Account", new { userId = c.UserId }), UserFirstName = up.FirstName, UserLastName = up.LastName, UserGravatarImageUrl = GravatarHasher.GetImageUrlForEmailAddress(up.EmailAddress) }).ToList() }; return(JsonSuccess(returnModel)); }