Using transactions in entity framework core

Using transactions in entity framework core

Using transactions in entity framework core

A database transaction is a sequence of multiple operations performed on a database, and all served as a single logical unit of work. If the transaction is committed, all of the operations are successfully committed to the database. If the transaction is rolled back, none of the operations are committed to the database.

In Entity Framework, the SaveChanges() method is responsible for internally creating a transaction that wraps all INSERT, UPDATE, and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction.

Default Transaction Behavior :

By default, if the database provider supports transactions, all changes in a single call to SaveChanges are committed in a transaction. If any of the changes fail, then the transaction is rolled back and none of the changes are committed to the database. This means that SaveChanges is guaranteed to either completely succeed, or leave the database unmodified if an error occurs.

Multiple SaveChanges in a Single Transaction :

Entity Framework Core and Entity Framework 6 provide support to create or use a single transaction with multiple SaveChanges() calls using the methods mentioned below:

a) DbContext.Database.BeginTransaction(): This method creates a new transaction for the database and allows us to commit or roll back changes made to the database using multiple SaveChanges method calls.

b) DbContext.Database.UseTransaction(): This method provides support to pass an existing transaction object created out of the scope of a context object. This will allow EF to execute commands within an external transaction object. Alternatively, pass in null to clear the framework’s knowledge of that transaction.

Savepoints :

When SaveChanges operations are performed multiple times within a transaction, Entity Framework core automatically creates a savepoint for every SaveChanges call.
In case SaveChanges() fails due to any reason, the transaction can be rolled back to the last successful Savepoint. After coming to the last savepoint, the transaction is in the same state as if the remaining part of the transaction has not been executed yet. Savepoints can be created manually and have user-defined names.

Usage of Savepoints :


a) CreateSavepoint(“NameString”) :
 This method creates a savepoint in the transaction. This allows restoring the transaction state to what it was at the time of the savepoint creation.

b) RollbackToSavepoint(“NameString”): This method rolls back all commands that were executed after the specified savepoint was established.

Before Dot NET 3.5, Developers often used to write ADO.NET code to save or retrieve data from the database. Developers had to create Database connections and maintain them for various CRUD operations according to the business requirement. This was a cumbersome and error-prone process. Microsoft introduced a framework called “Entity Framework” to automate all these database-related activities for your application and its first version was released around 2008. Entity Framework Core 6 also known as EF Core 6 is the latest version of Microsoft’s Entity Framework (as of the publishing date of this post). It is an Object Relational Mapping (ORM) framework that is an enhanced version of ADO.NET. This ORM automatically handles the data storage and retrieval from the database. EF Core is very powerful and also very easy to learn and use in your projects.

Share this post