Open Closed

off unit of work feature #6990


User avatar
0
sunivycsm created

I am selecting user _identityUserRepository.

  var query = (await _identityUserRepository.GetQueryableAsync())
      .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
          x => x.Name != null &&
               x.Name.Contains(input.Filter));

But when I use the following code, it is automatically saved in the datatable.
foreach (var item in query)
{
     item.Name = item.Surname + item.Name;
}

Is there a way I can edit the data without automatically updating it to the database? Here is all my source information:

public virtual async Task<PagedResultDto<LookupDto<Guid>>> GetIdentityUserLookupAsync(LookupRequestDto input)
{
    var query = (await _identityUserRepository.GetQueryableAsync())
        .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
            x => x.Name != null &&
                 x.Name.Contains(input.Filter));

    foreach (var item in query)
    {
        item.Name = item.Surname + item.Name
    }

    var lookupData = await query.PageBy(input.SkipCount, input.MaxResultCount).ToDynamicListAsync<IdentityUser>();
    var totalCount = query.Count();
    return new PagedResultDto<LookupDto<Guid>>
    {
        TotalCount = totalCount,
        Items = ObjectMapper.Map<List<IdentityUser>, List<LookupDto<Guid>>>(lookupData)
    };
}
  • ABP Framework version: v7.4.1
  • UI Type: MVC
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:
Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

7 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    You can try :

    var query = (await _identityUserRepository.GetQueryableAsync())
          .AsNoTracking() // add this
          .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
              x => x.Name != null &&
                   x.Name.Contains(input.Filter));
    
    
    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    sunivycsm created

    Sorry, this code doesn't work.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    It seems like you are missing a namespace using.

    You can reference the Volo.Abp.EntityFrameworkCore package.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    sunivycsm created

    using CMCUNI_THUCTAP.NganhHocs; using Volo.Abp.Identity; using CMCUNI_THUCTAP.TrinhDos; using CMCUNI_THUCTAP.Khoas; using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Threading.Tasks; using System.Linq.Dynamic.Core; using Microsoft.AspNetCore.Authorization; using Volo.Abp; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using CMCUNI_THUCTAP.Permissions; using CMCUNI_THUCTAP.GiangViens; using MiniExcelLibs; using Volo.Abp.Content; using Volo.Abp.Authorization; using Volo.Abp.Caching; using Microsoft.Extensions.Caching.Distributed; using CMCUNI_THUCTAP.Shared;

    namespace CMCUNI_THUCTAP.GiangViens {

    [Authorize(CMCUNI_THUCTAPPermissions.GiangViens.Default)]
    public abstract class GiangViensAppServiceBase : CMCUNI_THUCTAPAppService
    {
        protected IDistributedCache&lt;GiangVienExcelDownloadTokenCacheItem, string&gt; _excelDownloadTokenCache;
        protected IGiangVienRepository _giangVienRepository;
        protected GiangVienManager _giangVienManager;
        protected IRepository&lt;Khoa, Guid&gt; _khoaRepository;
        protected IRepository&lt;TrinhDo, Guid&gt; _trinhDoRepository;
        protected IRepository&lt;IdentityUser, Guid&gt; _identityUserRepository;
        protected IRepository&lt;NganhHoc, Guid&gt; _nganhHocRepository;
    
        public GiangViensAppServiceBase(IGiangVienRepository giangVienRepository, GiangVienManager giangVienManager, IDistributedCache&lt;GiangVienExcelDownloadTokenCacheItem, string&gt; excelDownloadTokenCache, IRepository&lt;Khoa, Guid&gt; khoaRepository, IRepository&lt;TrinhDo, Guid&gt; trinhDoRepository, IRepository&lt;IdentityUser, Guid&gt; identityUserRepository, IRepository&lt;NganhHoc, Guid&gt; nganhHocRepository)
        {
            _excelDownloadTokenCache = excelDownloadTokenCache;
            _giangVienRepository = giangVienRepository;
            _giangVienManager = giangVienManager; _khoaRepository = khoaRepository;
            _trinhDoRepository = trinhDoRepository;
            _identityUserRepository = identityUserRepository;
            _nganhHocRepository = nganhHocRepository;
        }
    
        public virtual async Task&lt;PagedResultDto&lt;GiangVienWithNavigationPropertiesDto&gt;> GetListAsync(GetGiangViensInput input)
        {
            var totalCount = await _giangVienRepository.GetCountAsync(input.FilterText, input.MaGiangVien, input.TenGiangVien, input.KhoaId, input.TrinhDoId, input.IdentityUserId, input.NganhHocId);
            var items = await _giangVienRepository.GetListWithNavigationPropertiesAsync(input.FilterText, input.MaGiangVien, input.TenGiangVien, input.KhoaId, input.TrinhDoId, input.IdentityUserId, input.NganhHocId, input.Sorting, input.MaxResultCount, input.SkipCount);
    
            return new PagedResultDto&lt;GiangVienWithNavigationPropertiesDto&gt;
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map&lt;List&lt;GiangVienWithNavigationProperties&gt;, List&lt;GiangVienWithNavigationPropertiesDto&gt;>(items)
            };
        }
    
        public virtual async Task&lt;GiangVienWithNavigationPropertiesDto&gt; GetWithNavigationPropertiesAsync(Guid id)
        {
            return ObjectMapper.Map&lt;GiangVienWithNavigationProperties, GiangVienWithNavigationPropertiesDto&gt;
                (await _giangVienRepository.GetWithNavigationPropertiesAsync(id));
        }
    
        public virtual async Task&lt;GiangVienDto&gt; GetAsync(Guid id)
        {
            return ObjectMapper.Map&lt;GiangVien, GiangVienDto&gt;(await _giangVienRepository.GetAsync(id));
        }
    
        public virtual async Task&lt;PagedResultDto&lt;LookupDto&lt;Guid&gt;>> GetKhoaLookupAsync(LookupRequestDto input)
        {
            var query = (await _khoaRepository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
                    x => x.TenKhoa != null &&
                         x.TenKhoa.Contains(input.Filter));
    
            var lookupData = await query.PageBy(input.SkipCount, input.MaxResultCount).ToDynamicListAsync&lt;Khoa&gt;();
            var totalCount = query.Count();
            return new PagedResultDto&lt;LookupDto&lt;Guid&gt;>
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map&lt;List&lt;Khoa&gt;, List&lt;LookupDto&lt;Guid&gt;>>(lookupData)
            };
        }
    
        public virtual async Task&lt;PagedResultDto&lt;LookupDto&lt;Guid&gt;>> GetTrinhDoLookupAsync(LookupRequestDto input)
        {
            var query = (await _trinhDoRepository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
                    x => x.TenTrinhDo != null &&
                         x.TenTrinhDo.Contains(input.Filter));
    
            var lookupData = await query.PageBy(input.SkipCount, input.MaxResultCount).ToDynamicListAsync&lt;TrinhDo&gt;();
            var totalCount = query.Count();
            return new PagedResultDto&lt;LookupDto&lt;Guid&gt;>
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map&lt;List&lt;TrinhDo&gt;, List&lt;LookupDto&lt;Guid&gt;>>(lookupData)
            };
        }
    
        public virtual async Task&lt;PagedResultDto&lt;LookupDto&lt;Guid&gt;>> GetIdentityUserLookupAsync(LookupRequestDto input)
        {
            var query = (await _identityUserRepository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
                    x => x.Name != null &&
                         x.Name.Contains(input.Filter));
    
            var lookupData = await query.PageBy(input.SkipCount, input.MaxResultCount).ToDynamicListAsync&lt;IdentityUser&gt;();
            var totalCount = query.Count();
            return new PagedResultDto&lt;LookupDto&lt;Guid&gt;>
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map&lt;List&lt;IdentityUser&gt;, List&lt;LookupDto&lt;Guid&gt;>>(lookupData)
            };
        }
    
        public virtual async Task&lt;PagedResultDto&lt;LookupDto&lt;Guid&gt;>> GetNganhHocLookupAsync(LookupRequestDto input)
        {
            var query = (await _nganhHocRepository.GetQueryableAsync())
                .WhereIf(!string.IsNullOrWhiteSpace(input.Filter),
                    x => x.TenNganh != null &&
                         x.TenNganh.Contains(input.Filter));
    
            var lookupData = await query.PageBy(input.SkipCount, input.MaxResultCount).ToDynamicListAsync&lt;NganhHoc&gt;();
            var totalCount = query.Count();
            return new PagedResultDto&lt;LookupDto&lt;Guid&gt;>
            {
                TotalCount = totalCount,
                Items = ObjectMapper.Map&lt;List&lt;NganhHoc&gt;, List&lt;LookupDto&lt;Guid&gt;>>(lookupData)
            };
        }
    
        [Authorize(CMCUNI_THUCTAPPermissions.GiangViens.Delete)]
        public virtual async Task DeleteAsync(Guid id)
        {
            await _giangVienRepository.DeleteAsync(id);
        }
    
        [Authorize(CMCUNI_THUCTAPPermissions.GiangViens.Create)]
        public virtual async Task&lt;GiangVienDto&gt; CreateAsync(GiangVienCreateDto input)
        {
            if (input.KhoaId == default)
            {
                throw new UserFriendlyException(L["The {0} field is required.", L["Khoa"]]);
            }
            if (input.TrinhDoId == default)
            {
                throw new UserFriendlyException(L["The {0} field is required.", L["TrinhDo"]]);
            }
    
            var giangVien = await _giangVienManager.CreateAsync(
            input.NganhHocIds, input.KhoaId, input.TrinhDoId, input.IdentityUserId, input.MaGiangVien, input.TenGiangVien
            );
    
            return ObjectMapper.Map&lt;GiangVien, GiangVienDto&gt;(giangVien);
        }
    
        [Authorize(CMCUNI_THUCTAPPermissions.GiangViens.Edit)]
        public virtual async Task&lt;GiangVienDto&gt; UpdateAsync(Guid id, GiangVienUpdateDto input)
        {
            if (input.KhoaId == default)
            {
                throw new UserFriendlyException(L["The {0} field is required.", L["Khoa"]]);
            }
            if (input.TrinhDoId == default)
            {
                throw new UserFriendlyException(L["The {0} field is required.", L["TrinhDo"]]);
            }
    
            var giangVien = await _giangVienManager.UpdateAsync(
            id,
            input.NganhHocIds, input.KhoaId, input.TrinhDoId, input.IdentityUserId, input.MaGiangVien, input.TenGiangVien, input.ConcurrencyStamp
            );
    
            return ObjectMapper.Map&lt;GiangVien, GiangVienDto&gt;(giangVien);
        }
    
        [AllowAnonymous]
        public virtual async Task&lt;IRemoteStreamContent&gt; GetListAsExcelFileAsync(GiangVienExcelDownloadDto input)
        {
            var downloadToken = await _excelDownloadTokenCache.GetAsync(input.DownloadToken);
            if (downloadToken == null || input.DownloadToken != downloadToken.Token)
            {
                throw new AbpAuthorizationException("Invalid download token: " + input.DownloadToken);
            }
    
            var giangViens = await _giangVienRepository.GetListWithNavigationPropertiesAsync(input.FilterText, input.MaGiangVien, input.TenGiangVien, input.KhoaId, input.TrinhDoId, input.IdentityUserId, input.NganhHocId);
            var items = giangViens.Select(item => new
            {
                MaGiangVien = item.GiangVien.MaGiangVien,
                TenGiangVien = item.GiangVien.TenGiangVien,
    
                Khoa = item.Khoa?.TenKhoa,
                TrinhDo = item.TrinhDo?.TenTrinhDo,
                IdentityUser = item.IdentityUser?.Name,
    
            });
    
            var memoryStream = new MemoryStream();
            await memoryStream.SaveAsAsync(items);
            memoryStream.Seek(0, SeekOrigin.Begin);
    
            return new RemoteStreamContent(memoryStream, "GiangViens.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        }
    
        public virtual async Task&lt;CMCUNI_THUCTAP.Shared.DownloadTokenResultDto&gt; GetDownloadTokenAsync()
        {
            var token = Guid.NewGuid().ToString("N");
    
            await _excelDownloadTokenCache.SetAsync(
                token,
                new GiangVienExcelDownloadTokenCacheItem { Token = token },
                new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(30)
                });
    
            return new CMCUNI_THUCTAP.Shared.DownloadTokenResultDto
            {
                Token = token
            };
        }
    }
    

    }

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    Sorry, I didn't get it.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    sunivycsm created

    @liangshiwei Here is my source code. When I add AsNoTracking I get the following error: Should I add any additional libraries?

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    liangshiwei created
    Fullstack Developer

    It seems like you are missing a namespace using.

    You can reference the Volo.Abp.EntityFrameworkCore package.

    https://support.abp.io/qa/questions/6990/3a11ba66-def0-421c-d1f2-39f9047dcb62

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on September 16, 2026, 14:50
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.