Enqueue only if DbContext.SaveChanges() succeeds

I’m trying to enqueue a job only if a set of changes are saved successfully by Entity Framework. The changes may fail because certain entities fail to validate. What’s the best way to achieve this?

Is there a way to enqueue the job so that it is saved by Entity Framework? Or should I just wrap the save and enqueue within a database transaction?

Thanks!

1 Like

Same here.

I expect that it is a very common use case to want to make application database updates and job enqueuing atomic.

E.g. Inside a single transaction, create a new entity and persist it using Entity Framework, and enqueue an EntityCreation job in hangfire (usign same database) to trigger further actions.

I would love to see an article documenting how to get .NET transactions with EF and Hangfire all working correctly together and highlighting any gotchas to look out for.

1 Like