0

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.

Sarah
  • 1
  • 2
  • 1
    Is the `DbContext` injected? From where are you calling your `GetCompanyBaseDataAsync` method? – Christian.K Oct 26 '21 at 13:28
  • I am calling the method from a service like that: var result = await _masterDataRepository.GetCompanyBaseDataAsync(companyKeys); – Sarah Oct 26 '21 at 13:38

1 Answers1

0

Remove static in your repository. It is a bug. You have to add DI to your startup like this

services.AddDbContext<DataContext>(options => {
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"));

otherwise you will have to create it manually

 private DataContext _context= new DataContext();
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thank you for your answer. I removed static but it still won't work. No Im creating it manually like you said – Sarah Oct 28 '21 at 08:04
  • Is it better to create it like you said or shuld I use using: `await using var context = new DataContext() ?` – Sarah Oct 28 '21 at 08:23