Categories: ASP.NET CoreEF Core

CRUD – Transaction Behavior in Entity Framework Core

EF – TransactionScope Tutorial

Step 1. Create the new interface class file under the Core project and the new repository class file under the Infra project. New interface class file name sets “ITransactionScopeRepository.” New repository class file name sets “TransactionScopeRepository.”

Fig 9. Core project and Infra project solution explorer – Transaction Scope Part

Create the CRUD methods in the ITransactionScopeRepository file.

public interface ITransactionScopeRepository<T> where T : class
{
 /// <summary>
 /// GetAll
 /// </summary>
 /// <returns>IEnumerable<T></returns>
 Task<IEnumerable<T>> GetAll();
 /// <summary>
 /// Get information by ID
 /// </summary>
 /// <param name="id">int32</param>
 /// <returns></returns>
 Task<T> GetById(int id);
 /// <summary>
 /// Create new data
 /// </summary>
 /// <param name="entity">entity model class</param>
 /// <returns></returns>
 Task Create(T entity);
 /// <summary>
 /// Update data
 /// </summary>
 /// <param name="entity">entity model class</param>
 /// <returns></returns>
 Task Update(T entity);
 /// <summary>
 /// Delete data
 /// </summary>
 /// <param name="entity">entity model class</param>
 /// <returns></returns>
 Task Delete(T entity);
}

Step 2. Add the CURD method in the ITransactionScopeRepository interface class file and the TransactionScopeRepository repository class file.

Fig 10. The TransactionScopeRepository file source code.

Note: If you don’t know how to add the generic type interface class in the controller files or how to use the generic type interface class file, please follow the “Generic Type in Repository – .NET Core Part.

Step 3. Create a new controller file and view files under the web project. New controller file name sets “TransactionScopeController.”

Fig 11. Web project solution explorer – Transaction Scope part.

Step 4. Add the repository interface class file to the controller file.

Fig 12. TransactionScopeController file source code.

Step 5. Add AOP in the controller file.

Fig 13. TransactionScopeController adds the AOP.

Note: If you don’t know how to create the aspect-oriented programming in .NET core case, please follow the “Aspect-oriented programming in .NET Core.

Step 6. Turn on the MS SQL profiler, then run the MS SQL profiler and the web application project under the debug model. Remember a thing that doesn’t close the MS SQL profiler windows.

Fig 14 SQL Profiler running.

Step 7. The MS SQL profiler will show all trace events when you click any button or load the initial page. It can let the developer understand the web application running logic between the web application and the MS SQL.

Fig 15 SQL Profile result – Transaction Scope part.

Conclusion

We demo the two method cases in the .NET Core case, such as the TransactionScope methods and the BeginTransaction method, then using MS SQL profiler sees the trace transaction events. We can understand these methods are from System.Transactions to apply, and these methods executing speed have a few difference that we can describe that non-distributed transaction and distributed transactions difference parts. In the MSDN article, we know the TransactionScope supports the distributed transactions where multiple databases involved a single transaction and non-distributed transaction. Database.BeginTransaction only supports non-distributed transactions( a local transaction where all actions are done with a single database.)

Reference

Page: 1 2 3

davidsky69

Recent Posts

Behavioral Patterns – Template method

Template method – It means the program defines an algorithm skeleton in the superclass, which lets subclasses…

4 years ago

Behavioral Patterns – Strategy

Strategy – It allows the programs to define a family of an algorithm, put each…

4 years ago

Behavioral Patterns – State

State – This pattern can let the program’s object alter its behavior when its internal state changes,…

4 years ago

Behavioral Patterns – Observer

Observer – It lets the programs define a subscription mechanism to notify multiple objects, which means when…

4 years ago

Behavioral Patterns – Null Object

Null Object – Designed to act as a default value of an object. From Design Pattern –…

4 years ago

Behavioral Patterns – Memento

Memento – It allows the programs to save and restore the object’s previous state without revealing its…

4 years ago