In addition, API calls which contacted the database would fail if you had started a transaction on the underlying database connection on your own. Since a transaction can only be started on an open connection, this meant that the only way a user could wrap several operations into one transaction was either to use a TransactionScope or use the ObjectContext.Connection property and start calling Open() and BeginTransaction() directly on the returned Entit圜onnection object. Prior to EF6 Entity Framework insisted on opening the database connection itself (it threw an exception if it was passed a connection that was already open). However some users require greater control over their transactions – this is covered in the following sections.
Smartsvn see more transactions code#
This default functionality is suitable for a lot of users and if so there is no need to do anything different in EF6 just write the code as you always did. By default, for instance, on SQL Server this is READ COMMITTED.Įntity Framework does not wrap queries in a transaction. In either case, the isolation level of the transaction is whatever isolation level the database provider considers its default setting. Also in EF6 execution of stored procedures included in the model through APIs such as ObjectContext.ExecuteFunction() does the same (except that the default behavior cannot at the moment be overridden). There are overloads of this method that allow you to override this behavior if you wish. Starting with EF6 Database.ExecuteSqlCommand() by default will wrap the command in a transaction if one was not already present. When you execute another such operation a new transaction is started. This transaction lasts only long enough to execute the operation and then completes.
Smartsvn see more transactions update#
In all versions of Entity Framework, whenever you execute SaveChanges() to insert, update or delete on the database the framework will wrap that operation in a transaction.
This document will describe using transactions in EF6 including the enhancements we have added since EF5 to make working with transactions easy.
If you are using an earlier version, some or all of the information does not apply. discussed in this page were introduced in Entity Framework 6. EF6 Onwards Only - The features, APIs, etc.