I am trying to get some data from a repository using DbContext. For some reason I keep getting a System.ObjectDisposedException. In my whole code I am not disposing anything and I do not use using statements in the concerning class. This is an example of a method where the exception is thrown, though this happens with all methods in this repository.
public async Task<List<Unternm>> GetCompanyBaseDataAsync(List<int> companyKeys)
{
return await _context.Set<Unternm>().Where(u => companyKeys.Contains(u.Baseid)).ToListAsync();
}
DB Context is injected like this:
public class MasterDataRepository : IMasterDataRepository
{
private readonly IMapper _mapper;
private static IDataContext _context;
public MasterDataRepository(IMapper mapper, IDataContext context)
{
_mapper = mapper;
_context = context;
}
This is what my data Context looks like:
public class DataContext : DbContext, IDataContext
{
public DataContext(DbContextOptions options) : base(options)
{
}
public DbSet<Arztlfd> Arztlfd { get; set; }
public DbSet<Arztstam> Arztstam { get; set; }
public DbSet<Oposten> Oposten { get; set; }
public DbSet<Unternm> Unternehmen { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseNpgsql(...);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
}
And this is the exception I am getting:
Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it 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: 'DataContext'.
Please can someone help? Thanks in advance.