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 mode开发者_运维技巧l 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?
You can use the TransactionScope class. This will ensure an Atomic transaction
using (TransactionScope scope = new TransactionScope())
{
mySP.Insert();
context.SaveChanges();
scope.Complete();
}
Check this out
Entity Framework - Using Transactions or SaveChanges(false) and AcceptAllChanges()?
精彩评论