title: How to use ApplicationDbContext in a Class / Separate Thread ASP.Net Core
body: <p>I am attempting to have a separate thread start a looping process to collect and import data from an api call and insert it into a local db. I attempt to create the database connection the same way I do in my controllers but it keeps giving me various errors.</p>
  
  <p>I have tried multiple ways. For the setup below it gives me this 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 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.'</p>
  </blockquote>
  
  <p>The setup is for testing purposes.</p>
  
  <pre><code>    public class HomeController : Controller
      {
          public ApplicationDbContext db;
  
          public HomeController(ApplicationDbContext context)
          {
              db = context;
          }
  
          public IActionResult Index()
          {
              // This executes the code and inserts the test row no problem.
              BinanceData binanceData = new BinanceData();
              binanceData.Symbol = "Test";
              binanceData.RealTime = DateTime.Now;
              db.BinanceData.Add(binanceData);
              db.SaveChanges();
              // End
  
              // Where I create the new thread and start the process
              Thread doThis = new Thread(delegate ()
              {
                  DataCollection dataCollection = new DataCollection(db);
                  dataCollection.InsertData(DateTime.Now);
              });
              doThis.Start();
              // End
  
              return View();
          }
      }
  </code></pre>
  
  <p>This is the class where I attempt to create a connection (or pass existing connection) and to begin the loop and collect/insert data.</p>
  
  <pre><code>    public class DataCollection
      {
          public ApplicationDbContext db;
  
          public DataCollection(ApplicationDbContext context)
          {
              db = context;
          }
  
          public void InsertData(DateTime nextTime)
          {
              List&lt;string&gt; tokens = new List&lt;string&gt; { "ETHBTC", "LTCBTC", "BNBBTC", "NEOBTC", "GASBTC", "BTCUSDT", "MCOBTC", "WTCBTC", "LRCBTC", "QTUMBTC", "YOYOBTC", "OMGBTC", "ZRXBTC", "STRATBTC", "SNGLSBTC", "BQXBTC", "KNCBTC", "FUNBTC", "SNMBTC", "IOTABTC", "LINKBTC", "XVGBTC", "SALTBTC", "MDABTC", "MTLBTC", "SUBBTC", "EOSBTC", "SNTBTC", "ETCBTC", "MTHBTC", "ENGBTC", "DNTBTC", "ZECBTC", "BNTBTC", "ASTBTC", "DASHBTC", "OAXBTC", "BTGBTC", "EVXBTC", "REQBTC", "VIBBTC", "TRXBTC", "POWRBTC", "ARKBTC", "XRPBTC", "MODBTC", "ENJBTC", "STORJBTC", "KMDBTC", "RCNBTC", "NULSBTC", "RDNBTC", "XMRBTC", "DLTBTC", "AMBBTC", "BATBTC", "BCPTBTC", "ARNBTC", "GVTBTC", "CDTBTC", "GXSBTC", "POEBTC", "QSPBTC", "BTSBTC", "XZCBTC", "LSKBTC", "TNTBTC", "FUELBTC", "MANABTC", "BCDBTC", "DGDBTC", "ADXBTC", "ADABTC", "PPTBTC", "CMTBTC", "XLMBTC", "CNDBTC", "LENDBTC", "WABIBTC", "TNBBTC", "WAVESBTC", "GTOBTC", "ICXBTC", "OSTBTC", "ELFBTC", "AIONBTC", "NEBLBTC", "BRDBTC", "EDOBTC", "WINGSBTC", "NAVBTC", "LUNBTC", "APPCBTC", "VIBEBTC", "RLCBTC", "INSBTC", "PIVXBTC", "IOSTBTC", "STEEMBTC", "NANOBTC", "VIABTC", "BLZBTC", "AEBTC", "NCASHBTC", "POABTC", "ZILBTC", "ONTBTC", "STORMBTC", "XEMBTC", "WANBTC", "WPRBTC", "QLCBTC", "SYSBTC", "GRSBTC", "CLOAKBTC", "GNTBTC", "LOOMBTC", "REPBTC", "TUSDBTC", "ZENBTC", "SKYBTC", "CVCBTC", "THETABTC", "IOTXBTC", "QKCBTC", "AGIBTC", "NXSBTC", "DATABTC", "SCBTC", "NPXSBTC", "KEYBTC", "NASBTC", "MFTBTC", "DENTBTC", "ARDRBTC", "HOTBTC", "VETBTC", "DOCKBTC", "POLYBTC", "PHXBTC", "HCBTC", "GOBTC", "PAXBTC", "RVNBTC", "DCRBTC", "USDCBTC", "MITHBTC", "BCHABCBTC" };
  
              foreach (string token in tokens)
              {
                  BinanceData binance = new BinanceData();
                  using (var client = new BinanceClient())
                  {
                      var data = client.GetKlines(token, Binance.Net.Objects.KlineInterval.OneMinute, null, null, 2);
                      var kline = data.Data[0];
                      binance.Symbol = token;
                      binance.Close = kline.Close;
                      binance.CloseTime = kline.CloseTime;
                      binance.High = kline.High;
                      binance.Low = kline.Low;
                      binance.Open = kline.Open;
                      binance.OpenTime = kline.OpenTime;
                      binance.QuoteAssetVolume = kline.QuoteAssetVolume;
                      binance.TakerBuyBaseAssetVolume = kline.TakerBuyBaseAssetVolume;
                      binance.TakerBuyQuoteAssetVolume = kline.TakerBuyQuoteAssetVolume;
                      binance.TradeCount = kline.TradeCount;
                      binance.Volume = kline.Volume;
                      binance.RealTime = DateTime.Now;
                  }
              db.BinanceData.Add(binance);
              db.SaveChanges();
              }
              CountUntilNextMin(nextTime);
          }
  
          public void CountUntilNextMin(DateTime nextTime)
          {
              while (DateTime.Now &lt; nextTime)
              {
                  System.Threading.Thread.Sleep(10000);
              }
              DateTime passTime = DateTime.Now.AddMinutes(1);
              InsertData(passTime);
          }
      }
  </code></pre>
  
  <p>Here is my ApplicationDbContext Class</p>
  
  <pre><code>    public class ApplicationDbContext : IdentityDbContext
      {
          public DbSet&lt;ApplicationUser&gt; ApplicationUsers { get; set; }
          public DbSet&lt;BinanceData&gt; BinanceData { get; set; }
          public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
              : base(options)
          {
          }
  
          public ApplicationDbContext()
              : base()
          {
          }
      }
  </code></pre>
  
  <p>And my StartUp.cs ConfigureServices Method</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
          {
              // This lambda determines whether user consent for non-essential cookies is needed for a given request.
              options.CheckConsentNeeded = context =&gt; true;
              options.MinimumSameSitePolicy = SameSiteMode.None;
          });
  
          services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
              options.UseSqlServer(
                  Configuration.GetConnectionString("DefaultConnection")));
          services.AddIdentity&lt;IdentityUser, IdentityRole&gt;()
              .AddDefaultUI()
              .AddDefaultTokenProviders()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
          services.AddMvc()
              .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
      }
  </code></pre>