I am getting the following error. Currently following the Async/await pattern And using IServiceScopeFactory. Trying to link the Repo with IQueryable.
Receiving Error below:
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'PropertyContext'.
Repository:
public class AddressRepository
{
private readonly IServiceScopeFactory _factory;
public AddressRepository(IServiceScopeFactory factory) : base(factory)
{
_factory = factory;
[EnableQuery]
public IQueryable<LkAddressType> GetAddressTypes()
{
using (var scope = _factory.CreateScope())
{
var db = scope.ServiceProvider.GetService<PropertyContext>();
return db.LkAddressType.AsNoTracking();
}
}
App Service:
public async Task<AddressTypeResponse> GetAddressTypes()
{
var data = await (_addressRepository.GetAddressTypes()).ToListAsync();
var mapped = _mapper.Map<ICollection<AddressTypeModel>>(data);
var dto = _mapper.Map<ICollection<AddressTypeDto>>(mapped);
return new AddressTypeResponse {Body = dto};
}
Controller:
[HttpGet("AddressType")]
public async Task<ActionResult<AddressTypeResponse>> GetAddressTypes()
{
return Ok(await _service.GetAddressTypes());
}
Currenly using Net Core 2.2, Entity Framework Core, and OData.