title: How to configure Hangfire with Mysql storage
body: <p>I have a web app that I developed using ASP.NET CORE and I want to send weekly newsletters to my customers. After looking around, I thought Hangfire will be a good tool for me to use.
  Unfortunately, I can't seem to get it to work using Mysql database. It works perfectly with the InMemboryStorage.</p>
  
  <p>Below is my set up.</p>
  
  <p>ASP.NET CORE 2.2</p>
  
  <p>Hangfire 1.7.8</p>
  
  <p>Hangfire.Mysql.Core 2.2.5</p>
  
  <p>Hangfire creates about 9 tables in the database, but when I try creating a background job,. I get an error that says, "hangfire_state table doesn't exist".</p>
  
  <p>Below is my configuration in the startup, and the error's stack trace.
  Please help out if you have any idea why this is happening. </p>
  
  <p>Thanks!</p>
  
  <pre><code>services.AddHangfire(cfg =&gt;
              {
  //                cfg.UseMemoryStorage();
                  cfg.UseStorage(new MySqlStorage(
                      "server=localhost; database=hangfire; password=''; uid=root; port=3306; Allow User Variables=True"));
              });
  </code></pre>
  
  <p>The controller action that creates a background job for testing</p>
  
  <pre><code>[HttpGet("/hagnfire-test")]
          public IActionResult TestHangfire()
          {
              BackgroundJob.Enqueue(() =&gt; Console.WriteLine("Hangfire works!"));
  
              return Ok("Hangfire test");
          }
  </code></pre>
  
  <p>Below is the error;</p>
  
  <pre><code>MySqlException: Table 'hangfire.hangfire_state' doesn't exist
  MySqlConnector.Core.ResultSet.ReadResultSetHeaderAsync(IOBehavior ioBehavior) in ResultSet.cs, line 49
  
  MySqlException: Table 'hangfire.hangfire_state' doesn't exist
  MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs, line 116
  
  BackgroundJobClientException: Background job creation failed. See inner exception for details.
  Hangfire.BackgroundJobClient.Create(Job job, IState state)
  </code></pre>
  
  <p>The stack trace</p>
  
  <pre><code>MySqlException: Table 'hangfire.hangfire_state' doesn't exist
  MySql.Data.MySqlClient.MySqlDataReader.ActivateResultSet() in MySqlDataReader.cs
  MySql.Data.MySqlClient.MySqlDataReader.CreateAsync(CommandListPosition commandListPosition, ICommandPayloadCreator payloadCreator, IDictionary&lt;string, CachedProcedure&gt; cachedProcedures, IMySqlCommand command, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlDataReader.cs
  MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList&lt;IMySqlCommand&gt; commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in CommandExecutor.cs
  MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQueryAsync(IOBehavior ioBehavior, CancellationToken cancellationToken) in MySqlCommand.cs
  MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() in MySqlCommand.cs
  Dapper.SqlMapper.ExecuteCommand(IDbConnection cnn, ref CommandDefinition command, Action&lt;IDbCommand, object&gt; paramReader) in SqlMapper.cs
  Dapper.SqlMapper.ExecuteImpl(IDbConnection cnn, ref CommandDefinition command) in SqlMapper.cs
  Dapper.SqlMapper.Execute(IDbConnection cnn, string sql, object param, IDbTransaction transaction, Nullable&lt;int&gt; commandTimeout, Nullable&lt;CommandType&gt; commandType) in SqlMapper.cs
  Hangfire.MySql.Core.MySqlWriteOnlyTransaction+&lt;&gt;c__DisplayClass7_0.&lt;SetJobState&gt;b__0(MySqlConnection x)
  Hangfire.MySql.Core.MySqlWriteOnlyTransaction.&lt;Commit&gt;b__30_0(MySqlConnection connection)
  Hangfire.MySql.Core.MySqlStorage+&lt;&gt;c__DisplayClass19_0.&lt;UseTransaction&gt;b__0(MySqlConnection connection)
  Hangfire.MySql.Core.MySqlStorage+&lt;&gt;c__DisplayClass20_0&lt;T&gt;.&lt;UseTransaction&gt;b__0(MySqlConnection connection)
  Hangfire.MySql.Core.MySqlStorage.UseConnection&lt;T&gt;(Func&lt;MySqlConnection, T&gt; func)
  Hangfire.MySql.Core.MySqlStorage.UseTransaction&lt;T&gt;(Func&lt;MySqlConnection, T&gt; func, Nullable&lt;IsolationLevel&gt; isolationLevel)
  Hangfire.MySql.Core.MySqlStorage.UseTransaction(Action&lt;MySqlConnection&gt; action)
  Hangfire.Client.CoreBackgroundJobFactory+&lt;&gt;c__DisplayClass14_0.&lt;Create&gt;b__3(int attempt)
  Hangfire.Client.CoreBackgroundJobFactory+&lt;&gt;c__DisplayClass15_0.&lt;RetryOnException&gt;b__0(int attempt)
  Hangfire.Client.CoreBackgroundJobFactory.RetryOnException&lt;T&gt;(ref int attemptsLeft, Func&lt;int, T&gt; action)
  Hangfire.Client.CoreBackgroundJobFactory.RetryOnException&lt;T&gt;(ref int attemptsLeft, Func&lt;int, T&gt; action)
  Hangfire.Client.CoreBackgroundJobFactory.RetryOnException(ref int attemptsLeft, Action&lt;int&gt; action)
  Hangfire.Client.CoreBackgroundJobFactory.Create(CreateContext context)
  Hangfire.Client.BackgroundJobFactory+&lt;&gt;c__DisplayClass12_0.&lt;CreateWithFilters&gt;b__0()
  Hangfire.Client.BackgroundJobFactory.InvokeClientFilter(IClientFilter filter, CreatingContext preContext, Func&lt;CreatedContext&gt; continuation)
  Hangfire.Client.BackgroundJobFactory.Create(CreateContext context)
  Hangfire.BackgroundJobClient.Create(Job job, IState state,
  </code></pre>