Search code examples
entity-frameworkwcftransactionsunity-containerentity-framework-6.1

Entity Framework Transaction across multiple SaveChanges


I am creating an application which uses WCF (4.5), EF (6.1), Unity (3.5) and Unity3.Wcf (3.5)

The application needs to run a monthly process which checks for changes that have happened in last month and create a record for an approval process.

This process will be triggered by a call to a WCF service method.

This is the basic logic:

Get collection of Things

For each Thing:

    Get collection of ThingChanges

    Calculate changed Amount

    Create New ThingApproval

    Update each ThingChange in ThingChanges with ThingApproval.ID

Now, as far as I am aware, in order to get ThingApproval.ID, I need to do SaveChanges after Create New ThingApproval which will populate with the ID from the DB. I then need to do a further SaveChanges either after each Update or once after the for each completes to commit all the updates.

If any part of this process fails, it needs to rollback ALL changes, back to before the first SaveChanges

How can I implement this?


Solution

  • I ended up implementing the GNaP.Data.Scope.EntityFramework package which gives full control of the DB Context, including transactional.