transaction in the Entity Framework
What is a transaction in the Entity Framework?
Entity Framework internally maintains transactions when the SaveChanges() method is called. The Entity Framework maintains a transaction for the multiple entity insert, update, and delete in a single SaveChanges() method. When we execute another operation, the Entity Framework creates a new transaction.
Entity Framework, whenever you execute SaveChanges() to insert, update or delete the DatabaseDatabase, the framework will wrap that operation in a transaction. When you invoke SaveChanges, the context automatically starts a transaction and commits or rolls it back depending on whether the persistence succeeded.
- This is all transparent to you, and you’ll never need to deal with it.
- This transaction lasts only long enough to execute the operation and then complete it.
- When you execute another such operation, a new transaction starts.
Entity Framework 6 provides the following :
- It is a simple and more straightforward method within an existing DbContext to start and complete transactions for users.
- It allows several operations to be combined within the same transaction, and hence either all are committed or all are rolled back as one.
- It also allows the user to specify the isolation level for the transaction more easily.
- It allows the DbContext to use a transaction that was started outside of the Entity Framework.
- Beginning a transaction requires that the underlying store connection is open.
- They are so calling DatabaseDatabase.BeginTransaction() will open the connection if it is not already opened.
- If DbContextTransaction opens the connection, it will close it when Dispose() is called.