TRANSACTION IN ENTITY FRAMEWORK.

Abayomi Omosehin
2 min readAug 31, 2020
Broken Transaction

Most senior software engineers will be familiar with the concept of transaction in SQL server. This is article is targeted to audience that are not aware or that has not encounter a problem that demands the use of transaction.

In one way or the other we had wanted to perform an INSERT, UPDATE,DELETE or run a stored procedure whose out put is needed to run another query that will end up saving to the database. Most at times we found out that the first transaction is not successful hence the second process cannot run. So what do we do ? This brought about the concept of TRANSACTION.

What is transaction? It is a single unit of work. A Unit of Work is a well-known design pattern that keeps track (In-Memory) of everything you do during a business transaction that can affect the database. This transaction lasts only long enough to execute the operation and then completes. When you execute another such operation a new transaction is started. When you’re done, it figures out everything that needs to be done to alter the database as a result of your work (martinfowler). The unit of work handles multiple requests at once. It maintains and stored multiple request (multiple inserts, delete or update) internally and send them as one transaction to the database.

A common example of a unit of work is the occasional money transfer we in our banks. Let say customer A want to send Customer B some X naira of money through Bank Q. The bank will deduct the money from customer A and update the balance for customer A, the transaction proceeds by crediting customer B with the X naira and update customer B balance. The transaction for these two processes either fail or succeed.

Each transaction is explicitly start with a BEGIN transaction statement and explicitly ends with a COMMIT or a ROLLBACK statement. If all the statement to the database succeeded the transaction will be committed but if any of the processes failed the transaction will be rollback to the safe state. In my next article we will have a practical coding example that depict transaction.
For the coding practical aspect check my second post
https://medium.com/@omosehin14/transaction-with-entity-framework-2-bcea2bbb01d9

Reference : https://docs.microsoft.com/en-us/ef/ef6/saving/transactions

--

--