title: Cannot create commands from unopened database
body: <p>I've searched around quite a lot and I cannot find any answers to this.</p>
  
  <p>I am writing a Xamarin Forms Mobile application, it seems when I minimise the application and then reopen it or one of my activities get launched the following exception gets thrown:</p>
  
  <pre><code>SQLiteConnection.CreateCommand (System.String cmdText, System.Object[] ps)
  SQLite.SQLiteException: Cannot create commands from unopened database
  SQLiteConnection.CreateCommand (System.String cmdText, System.Object[] ps)
  TableQuery`1[T].GenerateCommand (System.String selectionList)
  TableQuery`1[T].GetEnumerator ()
  System.Collections.Generic.List`1[T]..ctor (System.Collections.Generic.IEnumerable`1[T] collection) [0x00062] in :0
  Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source)
  AsyncTableQuery`1[T].&lt;ToListAsync&gt;b__9_0 ()
  Task`1[TResult].InnerInvoke ()
  Task.Execute ()
  </code></pre>
  
  <p>Here is my code:</p>
  
  <p><strong>Generic Repository</strong> (Where the Sqlite instance gets created)</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : Entity, new()
  {
       private readonly SQLiteAsyncConnection _db;
  
      public Repository(string dbPath)
      {
          _db = new SQLiteAsyncConnection(dbPath);
          _db.CreateTableAsync&lt;T&gt;().Wait();
      }
  }
  </code></pre>
  
  <p><strong>The IOC registration</strong></p>
  
  <pre><code>FreshIOC.Container.Register&lt;IRepository&lt;Settings&gt;&gt;(new Repository&lt;Settings&gt;(dbPath)); // FreshIOC is a wrapper around TinyIOC
  </code></pre>
  
  <p><strong>In my App.xaml.cs OnResume</strong></p>
  
  <pre><code>protected override void OnResume()
  {
      SQLiteAsyncConnection.ResetPool();
  }
  </code></pre>
  
  <p>The above with <code>ResetPool</code> I put that in to see if it would make a difference but it did not.</p>
  
  <p><strong>URL Activity</strong></p>
  
  <pre><code>protected override void OnCreate(Bundle bundle)
  {
      base.OnCreate(bundle);
  
      var url = Intent.Data.ToString();
      var split = url.Split(new[] { "ombi://", "_" }, StringSplitOptions.RemoveEmptyEntries);
      if (split.Length &gt; 1)
      {
          var dbLocation = new FileHelper().GetLocalFilePath("ombi.db3");
          var repo = new Repository&lt;OmbiMobile.Models.Entities.Settings&gt;(dbLocation);
          var settings = repo.Get().Result;
          foreach (var s in settings)
          {
              var i = repo.Delete(s).Result;
          }
          repo.Save(new Settings
          {
              AccessToken = split[1],
              OmbiUrl = split[0]
          });
      }
  
      Intent startup = new Intent(this, typeof(MainActivity));
      StartActivity(startup);
      Finish();
  }
  </code></pre>
  
  <p>I am not sure what else to do or look for, I can't seem to find any information about this sort of error.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>After more debugging it seems to only happen after the Url activity has finished.
  I have removed the DB code from the Activity and it still seems to happen. Once the Activity has launched the main <code>App()</code> then runs this code:</p>
  
  <pre><code>var repo = FreshIOC.Container.Resolve&lt;IRepository&lt;Settings&gt;&gt;();
  try
  {
      Task.Run(async () =&gt;
      {
          settings = (await repo.Get()).FirstOrDefault();
      }).Wait();
  }
  catch (Exception e)
  {
      Debug.WriteLine(e.Message);
      throw;
  }
  </code></pre>
  
  <p>This where the error is happening. It happens when the <code>Get()</code> is called which calls <code>return _db.Table&lt;T&gt;().ToListAsync();</code></p>
  
  <p>I have tried making everything async (didn't help), making the repository, connection and where we do <code>CreateTableAsync</code> async and still no luck.</p>