Can I use EF Core 2.0 'AsNoTracking' in CRUD service GetAll?

asp.net-core aspnetboilerplate c# entity-framework entity-framework-core

Question

I have an Application Service that implements:

public interface IPracticeAppService : IAsyncCrudAppService<PracticeDto, int, PagedAndSortedResultRequestDto,CreatePracticeDto, PracticeDto>

I would like to use AsNoTracking.

In the example below, is there a way of using AsNoTracking?

public override Task<PagedResultDto<PracticeDto>> GetAll(PagedAndSortedResultRequestDto input)
{    
    return base.GetAll(input);
}
1
0
3/28/2018 4:52:29 AM

Accepted Answer

Yes. However, .AsNoTracking() only applies to IQueryable so you can't call the base method.

public override async Task<PagedResultDto<PracticeDto>> GetAll(PagedAndSortedResultRequestDto input)
{
    CheckGetAllPermission();

    var query = CreateFilteredQuery(input).AsNoTracking(); // Here!

    var totalCount = await AsyncQueryableExecuter.CountAsync(query);

    query = ApplySorting(query, input);
    query = ApplyPaging(query, input);

    var entities = await AsyncQueryableExecuter.ToListAsync(query);

    return new PagedResultDto<PracticeDto>(
        totalCount,
        entities.Select(MapToEntityDto).ToList()
    );
}
2
3/28/2018 4:46:23 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow