Carlo V. Dango
Carlo V. Dango

Reputation: 13852

Automatic persistence of state with MassTransit 4.0 saga with EntityFramework Core

Hi I've just started using MassTransit 4.0 with .net core and EntityFramework Core.

I have defined a state spanning several classes, which I have wired up in a 1-many relationship using

public class SagaInstanceMap3 : IEntityTypeConfiguration<InstanceState>
{
    public void Configure(EntityTypeBuilder<InstanceState> builder)
    {
        builder.HasKey(x => x.CorrelationId);
        builder.HasMany(x => x.Args);
        builder.HasMany(x => x.MoreArgs);
        builder.Property(x => x.RowVersion).IsRowVersion().IsRequired(); 
    }
}

When creating an initial state everything is stored in the DB, but when fetching state in the following transition, only the main table is fetched.

I've been able to remedy this using queryCustomization as seen below

   public static void Main()
    {
        var saga = new MySaga();

        var contextFactory = new SagaWithDependencyContextFactory();

        using (var context = contextFactory.CreateDbContext(Array.Empty<string>()))
        {
            context.Database.Migrate();
        }

        Func<DbContext> sagaDbContextFactory = () => contextFactory.CreateDbContext(Array.Empty<string>());

        var efSagaRepository =
            new EntityFrameworkSagaRepository<InstanceState>(sagaDbContextFactory,
                queryCustomization: q => q.Include(it => it.Args).ThenInclude(it => it.MoreArgs),
                optimistic: true); 

Unfortunately, this means that I have to remember to map all my relations twice! Is there at way to have the EF Core integration eagerly fetch all child tables upon transitions?

The EF6.0 integration with previous releases of MassTransit did not require me to do the queryCustomization trick.

Upvotes: 1

Views: 1312

Answers (2)

Carlo V. Dango
Carlo V. Dango

Reputation: 13852

Im not using Masstransit anymore but perhaps this project https://github.com/kbilsted/EFCoreEagerFetching can be of use for others in terms of defining the relationships only once.

Upvotes: 0

Alexey Zimarev
Alexey Zimarev

Reputation: 19630

It is because EF Core works differently than EF 6.0. This is why the query customisation was added.

As per your implementation, I think there is no other way unless you do something in the mapping.

MassTransit has nothing special in the EF Saga repository implementation, you can check the code yourself.

Upvotes: 1

Related Questions