0

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?

Shevek
  • 3,869
  • 5
  • 43
  • 63
  • possible duplicate of [Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?](http://stackoverflow.com/questions/815586/entity-framework-using-transactions-or-savechangesfalse-and-acceptallchanges) – Gert Arnold Oct 24 '14 at 11:32
  • @GertArnold that doesn't specifically answer my question as it doesn't take into account my use of DI/IoC - I am not explicitly creating any scope – Shevek Oct 24 '14 at 11:57
  • @GertArnold actually, I just read the second answer and that does seem to fit. – Shevek Oct 24 '14 at 12:00
  • @GertArnold actually that doesn't help - my context is held in a repository class which is called by the application class so it doesn't have access to the context to set the transaction – Shevek Oct 24 '14 at 12:47
  • "I am not explicitly creating any scope" No, that's the point. You should wrap everything in a `using TransactionScope` block, as in the accepted answer. – Gert Arnold Oct 24 '14 at 12:50
  • Last comment on the accepted answer: "This is no longer possible in EF 6.1. Do you know what kind of adjustments need to be made to work now?" – Shevek Oct 24 '14 at 15:08
  • That only applies to this AcceptChanges method. It's still possible by the way, but you have to "descend" to the `ObjectContext` http://stackoverflow.com/a/5655040/861716. – Gert Arnold Oct 24 '14 at 15:25

1 Answers1

0

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

Shevek
  • 3,869
  • 5
  • 43
  • 63