Home > Net >  Cannot access a disposed context instance Entity Framework
Cannot access a disposed context instance Entity Framework

Time:11-20

I'm making a graphql version of an api.

I have this code:

namespace WebApiAutores.GraphQL
{
    public class AutoresType : ObjectType<Autor>
    {
        public AutoresType()
        {

        }
    }

    public interface IAutoresProvider
    {
        Task<List<AutorDTO>> GetAutores();
    }

    public class AutoresProvider : ControllerBase, IAutoresProvider
    {
        private readonly ApplicationDbContext context;
        private readonly IMapper mapper;
        private readonly IHttpContextAccessor httpContextAccessor;

        public AutoresProvider(ApplicationDbContext context, IMapper mapper,IHttpContextAccessor httpContextAccessor)
        {
            this.context = context;
            this.mapper = mapper;
            this.httpContextAccessor = httpContextAccessor;
        }
        
        public async Task<List<AutorDTO>> GetAutores()
        {
            // get the header for pagination
            var pagina = httpContextAccessor.HttpContext.Request.Headers["pagina"];
            var cantidad1 = httpContextAccessor.HttpContext.Request.Headers["cantidad"];

            var pag = new PaginacionDTO()
            {
                Pagina = Int32.Parse(pagina),
                RecordsPorPagina = Int32.Parse(cantidad1)
            };

            var queryable = context.Autores.AsQueryable();

            

            var autores2 = queryable.Skip((pag.Pagina - 1) * pag.RecordsPorPagina).Take(pag.RecordsPorPagina);

            
            return mapper.Map<List<AutorDTO>>(autores2.ToList());



            
        }
        
    }

    

    [ExtendObjectType("Queries")]
    public class AutoresQuery
    {
        private readonly IAutoresProvider autoresProvider;

        public AutoresQuery(IAutoresProvider autoresProvider)
        {
            this.autoresProvider = autoresProvider;
        }

        public async Task<List<AutorDTO>> Autores()
        {

            return  await autoresProvider.GetAutores();

        }
    }

}

The problem is that the first time I make a request, It works properly but the second time I get this error:

Cannot access a disposed context instance...

The problem is in this line:

return mapper.Map<List<AutorDTO>>(autores2.ToList());

So far, I looked the others answers similar to this but I cannot understand why is not working the second time.

CodePudding user response:

Solved. The problem was that I forgot to register the AutoresProvider in the dependency injection system:

AddScoped<AutoresQuery>() // ** I forgot this line
services.AddGraphQLServer()
        .AddQueryType(q => q.Name("Queries"))
        .AddType<AutoresQuery>()
        .AddType<ProductQuery>()
        .ModifyRequestOptions(x=>x.IncludeExceptionDetails=true);

I mistakenly thought that it was enough with adding the query type in the GraphQL Server. Now it makes sense why it was disposing of the context after the first request.

  • Related