Base solution for your next web application
Open Closed

Issue with repository get with Guid #8460


User avatar
0
joe704la created

My project is a .NET Core 3.1 project with Angular front-end.

I have a simple entity called FileAttachment and I have tried sending in a guid to the below method in my FileAttachmentManager I created and it always comes back Null. Even though I can grab the Guid when debugging and write SQL that will pull up the record I am looking for, so I know the record is in the DB. I cannot for the life of me figure out why it is not working. The manager is in the Core layer and I am calling it from the ChatController.

public Task<FileAttachment> GetOrNullAsync(Guid id)
        {
            return _fileAttachmentRepository.FirstOrDefaultAsync(id);
        }
            I also tried creating a service in the Application layer doing something similar to the below method. But also did not work. Any help would be much appreciated. 
    
        public async Task<GetFileAttachmentForViewDto> GetUserFileAttachmentForView(Guid id)
        {
            var currentUserId = AbpSession.UserId;
            if (!currentUserId.HasValue)
            {
                throw new UserFriendlyException("User not logged in, please login to continue.");
            }

            var fileAttachment = await _fileAttachmentRepository.GetAsync(id);


            var output = new GetFileAttachmentForViewDto { FileAttachment = ObjectMapper.Map<FileAttachmentDto>(fileAttachment) };

            var input = new DownloadAttachmentInputDto
            {
                TypeId = fileAttachment.TypeId,
                FileAttachmentId = fileAttachment.Id,
                FileAttachmentType = fileAttachment.FileAttachmentType
            };

            await CheckDownloadPermissions(input, currentUserId.Value);

            return output;
        }

2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Please share FileAttachment's class code and database records. It may be filtered by filters. In addition, what is the TenantId of AbpSession?

  • User Avatar
    0
    joe704la created

    Figured the issue out. When you asked about the TenantId I had a thought and checked the ChatController and sure enough it uses a using statement that sets TentantId to null. I removed that and it worked just fine.

                using (CurrentUnitOfWork.SetTenantId(null))
                {
                    var fileDto = await _fileAttachmentsService.GetUserFileAttachmentForView(fileId);
                    var fileObject = fileDto.FileAttachment;
                    if (fileObject == null)
                    {
                        return StatusCode((int)HttpStatusCode.NotFound);
                    }
    
    
                    return File(fileObject.BinaryObject, contentType, fileName);
                }