Riz
Riz

Reputation: 7002

Entity Framework - SQL Server - Transactions handling

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?

Upvotes: 1

Views: 1576

Answers (2)

Eranga
Eranga

Reputation: 32447

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

using (TransactionScope scope = new TransactionScope())
{

     mySP.Insert();

     context.SaveChanges();

     scope.Complete();
}

Upvotes: 2

Related Questions