How to rollback a transaction in Entity Framework?

I believe (but I am no long time expert in EF) that until the call to context. SaveChanges goes through, the transaction is not started. I'd expect an Exception from that call would automatically rollback any transaction it started.

Alternatives (in case you want to be in control of the transaction) from J. Lerman's "Programming Entity Framework O'Reilly, pg. 618.

I believe (but I am no long time expert in EF) that until the call to context. SaveChanges goes through, the transaction is not started. I'd expect an Exception from that call would automatically rollback any transaction it started.

Alternatives (in case you want to be in control of the transaction) from J. Lerman's "Programming Entity Framework" O'Reilly, pg. 618 using (var transaction = new System.Transactions.

TransactionScope()) { try { context.SaveChanges(); transaction.Complete(); context. AcceptAllChanges(); } catch(OptimisticConcurrencyException e) { //Handle the exception context.SaveChanges(); } } or bool saved = false; using (var transaction = new System.Transactions. TransactionScope()) { try { context.SaveChanges(); saved = true; } catch(OptimisticConcurrencyException e) { //Handle the exception context.SaveChanges(); } finally { if(saved) { transaction.Complete(); context.

AcceptAllChanges(); } } }.

– Shimmy Jul 1 '09 at 17:37 In essence, I believe you get transaction rollback for free in the simple scenario, and that you will need to handle transactions as demonstrated in the two examples for more complex scenarios. Your sample app seems to confirm this. Sorry if my wording was less than stellar - was in-between meetings when I wrote this yesterday.

– FOR Jul 2 '09 at 11:38.

OK I created a sample a pplication like the rxample above (in the question) and afterwords I checked in the DB and no users were added. Conclusion: ObjectContext. SaveChange it's automatically a transaction.

Note: I believe transactions will be needed if executing sprocs etc.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions