1

I am using ASP.Net Web Forms, .Net Framework 3.5, Entity Framework In my application I am doing inserts using entity model and then calling SaveChanges(), I know in this case Entity model handles the transactions and if any query fails, everything will be reverted.

But in few cases, I use a SQL-Server SP to insert data in different tables. This SP has 4 to 5 insert queries. I want to know that if any one query in SP fails, will Entity model revert the other queries or not? I don't think entity model will handle that - right? Is there any workaround or I'll have to use Entity model to insert data for handling transactions?

2
  • Try changing your sp to fail on purpose halfway through and see if any changes persist. Commented Aug 22, 2011 at 6:53
  • If you have SP, you can handle the transaction in SP itself. I guess this is the better way. Commented Dec 23, 2013 at 12:53

2 Answers 2

2

You can use the TransactionScope class. This will ensure an Atomic transaction

using (TransactionScope scope = new TransactionScope())
{

     mySP.Insert();

     context.SaveChanges();

     scope.Complete();
}
Sign up to request clarification or add additional context in comments.

2 Comments

So if any insert/update or delete query in my SP fails, executed queries will be reverted too? Do I need to write something specific in my SP?
@eFriend your SP should throw an exception if it fails. if any one of the operations fail all will be reverted.
-1

Check this out

Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?

1 Comment

I have gone through that question already, but it doesn't answer my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.