Kirsten
Kirsten

Reputation: 18188

Doing transactions with Entity Framework Code First

There is an answer here to explain how to do transactions with Entity Framework However the solution does not work with code first. I have experimented and my tests indicate the following does work

using (var scope = new TransactionScope())
{
        DBContext1.SaveChanges()
        If (ForceFailure) return 0  // used in testing
        DBContext2.SaveChanges()
        scope.Complete();
 }

However I feel nervous because I am no longer passing parameters to SaveChanges or calling AcceptAllChanges

How do I establish whether I can trust my solution?

Upvotes: 4

Views: 2963

Answers (1)

tne
tne

Reputation: 7261

SaveChanges(Boolean) is part of ObjectContext, not DbContext. Note that the method SaveChanges(Boolean) in ObjectContext is deprecated in favor of SaveChanges(SaveOptions).

If you still want to use DbContext, you may be able to overload its own SaveChanges method to use IObjectContextAdapter.ObjectContext, by using an extension method for example.

Upvotes: 2

Related Questions