Updating multiple objects in single transaction in entity framework
I'm adding a set of objects using entity framework in single context but want to be in 开发者_JAVA技巧a transaction. How I can do that?
You may take a look at the How to: Manage Transactions in the Entity Framework article on MSDN. Basically you would use the TransactionScope class to demarcate the boundaries of the transaction.
If you call SaveChanges
only once for all your changes you already have transaction because SaveChanges
internally checks if the thread has associated transaction (for example via TransactionScope
) and if it doesn't it will internally create a new one by calling connection.BeginTransaction
.
You can create transaction object on context connection object, then you can use transaction commit or rollback with regular mechanism or transaction scope.
dbContext.Connection.BeginTransaction()
精彩评论