title: How to configure EF Core persistence in MassTransit and Automatonymous?
body: <p>I am trying to configure Automatonymous worker implementation with EF Core as persistence. I publish event via api and process it in hosted service using RabbitMq as a transport. Unfortunately database does not store state of machine. I applied migrations and I see table <code>OrderState</code>, but there is no data in it after I publish <code>OrderSubmmited</code> event. It gets processed properly, because I see a log in a console, but database table remains empty. What am I missing?</p>
  <p>This is my code:</p>
  <p>Event:</p>
  <pre><code>public interface OrderSubmitted : CorrelatedBy&lt;Guid&gt;
      {
      }
  </code></pre>
  <p>Consumer:</p>
  <pre><code>public class OrderSubmittedConsumer : IConsumer&lt;OrderSubmitted&gt;
      {
          public Task Consume(ConsumeContext&lt;OrderSubmitted&gt; context)
          {
              Console.Out.WriteLine($&quot;Order with id {context.Message.CorrelationId} has been submitted.&quot;);
              
              return Task.CompletedTask;
          }
      }
  </code></pre>
  <p>My saga instance:</p>
  <pre><code>public class OrderState : SagaStateMachineInstance
      {
          public Guid CorrelationId { get; set; }
          public string CurrentState { get; set; }
      }
  </code></pre>
  <p>State machine:</p>
  <pre><code>public class OrderStateMachine : MassTransitStateMachine&lt;OrderState&gt;
      {
          public State Submitted { get; private set; }
  
          public Event&lt;OrderSubmitted&gt; OrderSubmitted { get; set; }
  
          public OrderStateMachine()
          {
              Event(() =&gt; OrderSubmitted);
              
              InstanceState(x =&gt; x.CurrentState);
              
              Initially(
                  When(OrderSubmitted)
                      .TransitionTo(Submitted));
              
              DuringAny(
                  When(OrderSubmitted)
                      .TransitionTo(Submitted));
          }
      }
  </code></pre>
  <pre><code>public class OrderStateMachineDefinition : SagaDefinition&lt;OrderState&gt;
      {
          public OrderStateMachineDefinition()
          {
              ConcurrentMessageLimit = 15;
          }
      }
  </code></pre>
  <p>DbContext:</p>
  <pre><code>public class OrderStateDbContext : SagaDbContext
      {
          public OrderStateDbContext(DbContextOptions options) : base(options)
          {
          }
  
          protected override IEnumerable&lt;ISagaClassMap&gt; Configurations
          {
              get
              {
                  yield return new OrderStateMap();
              }
          }
      }
  </code></pre>
  <pre><code>public class OrderStateMap : SagaClassMap&lt;OrderState&gt;
      {
          protected override void Configure(EntityTypeBuilder&lt;OrderState&gt; entity, ModelBuilder model)
          {
              entity.Property(x =&gt; x.CurrentState).HasMaxLength(64);
          }
      }
  </code></pre>
  <p>Program class:</p>
  <pre><code>public class Program
      {
          public static void Main(string[] args)
          {
              CreateHostBuilder(args).Build().Run();
          }
  
          public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
              Host.CreateDefaultBuilder(args)
                  .ConfigureServices((hostContext, services) =&gt;
                  {
                      services.AddDbContext&lt;OrderStateDbContext&gt;(builder =&gt;
                           builder.UseSqlServer(&quot;Server=(localdb)\\mssqllocaldb;Database=OrderState;Trusted_Connection=True;&quot;, m =&gt;
                           {
                               m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name);
                               m.MigrationsHistoryTable($&quot;__{nameof(OrderStateDbContext)}&quot;);
                           }));
                      
                      services.AddMassTransit(config =&gt;
                      {
                          config.AddSagaRepository&lt;OrderState&gt;()
                              .EntityFrameworkRepository(r =&gt;
                              {
                                  r.ExistingDbContext&lt;OrderStateDbContext&gt;();
                                  r.LockStatementProvider = new SqlServerLockStatementProvider();
                              });
                          
                          config.AddConsumer&lt;OrderSubmittedConsumer&gt;();
  
                          config.UsingRabbitMq((ctx, cfg) =&gt; {
                              cfg.Host(&quot;amqp://guest:guest@localhost:5672&quot;);
                              cfg.ReceiveEndpoint(&quot;order-queue&quot;, c =&gt; {
                                  c.ConfigureConsumer&lt;OrderSubmittedConsumer&gt;(ctx);
                                  // I'm assuming this is the place where something like c.StateMachineSaga() is missing, but I don't know how should this look like with EF Core
                              });
                          });
                      });
                      
                      services.AddHostedService&lt;Worker&gt;();
                  });
      }
  </code></pre>
  <p>Worker class:</p>
  <pre><code>public class Worker : IHostedService
      {
          private readonly IBusControl _bus;
  
          public Worker(IBusControl bus)
          {
              _bus = bus;
          }
  
          public async Task StartAsync(CancellationToken cancellationToken)
          {
              await _bus.StartAsync(cancellationToken).ConfigureAwait(false);
          }
  
          public Task StopAsync(CancellationToken cancellationToken)
          {
              return _bus.StopAsync(cancellationToken);
          }
      }
  </code></pre>