Automatic persistence of state with MassTransit 4.0 saga with EntityFramework Core

.net-core entity-framework-core masstransit


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>()))

        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.

1/12/2018 10:05:10 AM

Accepted Answer

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.

1/15/2018 11:49:45 AM

Popular Answer

Im not using Masstransit anymore but perhaps this project can be of use for others in terms of defining the relationships only once.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow