title: Cannot access a disposed object in ASP.NET Core when injecting DbContext
body: <p>On an ASP.NET Core project I have the following on Startup:</p>
  
  <pre><code>  services.AddDbContext&lt;Context&gt;(x =&gt; x.UseSqlServer(connectionString));
  
    services.AddTransient&lt;IValidationService, ValidationService&gt;();
  
    services.AddTransient&lt;IValidator&lt;Model&gt;, ModelValidator&gt;();
  </code></pre>
  
  <p>The ValidationService is as follows:</p>
  
  <pre><code>public interface IValidationService {
      Task&lt;List&lt;Error&gt;&gt; ValidateAsync&lt;T&gt;(T model);
  }
  
  public class ValidationService : IValidationService {
      private readonly IServiceProvider _provider;
  
      public ValidationService(IServiceProvider provider) {
          _provider = provider;
      }
  
      public async Task&lt;List&lt;Error&gt;&gt; ValidateAsync&lt;T&gt;(T model) {
          IValidator&lt;T&gt; validator = _provider.GetRequiredService&lt;IValidator&lt;T&gt;&gt;();
  
          return await validator.ValidateAsync(model);
      }
  }
  </code></pre>
  
  <p>And the ModelValidator is as follows:</p>
  
  <pre><code>public class ModelValidator : AbstractValidator&lt;Model&gt; {
    public ModelValidator(Context context) {
      // Some code using context
    }
  }
  </code></pre>
  
  <p>When I inject a IValidationService in a controller and use it as:</p>
  
  <pre><code>List&lt;Error&gt; errors = await _validator.ValidateAsync(order);    
  </code></pre>
  
  <p>I get the error:</p>
  
  <blockquote>
    <p>System.ObjectDisposedException: 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 is 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: 'Context'.</p>
  </blockquote>
  
  <p>Any idea why I am having this error when using Context inside ModelValidator.</p>
  
  <p>How to fix this?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>So I changed the code to:</p>
  
  <pre><code>services.AddScoped&lt;IValidationService, ValidationService&gt;();
  
  services.AddScoped&lt;IValidator&lt;Model&gt;, ModelValidator&gt;();
  </code></pre>
  
  <p>But I get the same error ...</p>
  
  <p><strong>UPDATE - Seed Data Code inside Configure method on Startup</strong></p>
  
  <p>So on Configure method I have:</p>
  
  <pre><code>if (hostingEnvironment.IsDevelopment())
    applicationBuilder.SeedData();
  </code></pre>
  
  <p>And the SeedData extension is:</p>
  
  <pre><code>public static class DataSeedExtensions {
      private static IServiceProvider _provider;
  
      public static void SeedData(this IApplicationBuilder builder) { 
          _provider = builder.ApplicationServices;
          _type = type;
  
          using (Context context = (Context)_provider.GetService&lt;Context&gt;()) {
              await context.Database.MigrateAsync();
              // Insert data code
      }
  }
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p><strong>UPDATE - A possible solution</strong></p>
  
  <p>Changing my Seed method to the following seems to work:</p>
  
  <pre><code>using (IServiceScope scope = 
      _provider.GetRequiredService&lt;IServiceScopeFactory&gt;().CreateScope()) {
      Context context = _provider.GetService&lt;Context&gt;();
      // Insert data in database
  }
  </code></pre>