Categories: ASP.NET CoreEF Core

CRUD – Transaction Behavior in Entity Framework Core

EF – BeginTransaction Tutorial

Step 1. Create the new interface class files and the new repository class file. These files include the CRUD method. Remember the interface class files and the repository class files use the generic type description. The new interface class file name sets “ITransactionRepository.” The new repository class file name sets “TransactionRepository.”

Fig 2 Core project and Infra project solution explorer

In the new interface class file, adding the CRUD methods.

public interface ITransactionRepository<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. Edit the CRUD methods in the repository class file.

Fig 3 TransactionRepository repository class file source code

Note: If you want to see the complete source code, please go to my Github – Enterprise_MVC_WebApp. My Github link puts on my profile page.

Step 3. Create the view files under the web project’s Views folder. Create the new controller files under the Controller folder. New controller file name sets “TransactionController.”

Fig 4 Web project solution explorer

Note: If you want to know how to create these files, please follow the “Building MVC pattern project by Visual Studio 2017 ( ASP.Net Core Part ).”

Step 4. Add the repository’s interface class in the controller files and other page’s ActionResult method source code.

Fig 5. TransactionController 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 5. Add the AOP in a controller file. All of ActionResult add the AOP mechanism.

Fig 6. Add the aspect-oriented programming in the controller files

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 7. Running SQL profiler

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 8 SQL profiler result

Page: 1 2 3

davidsky69

Recent Posts

SOLID Principles – Single-responsibility principle

The single-responsibility principle (SRP) is a computer-programming principle that states that every module, class or…

4 years ago

Microservices – Command and Query Responsibility Segregation(CQRS)

Command query responsibility segregation (CQRS) generalizes CQS to message-driven and event-driven architectures: it applies the CQS…

4 years ago

Microservices – Domain-driven design(DDD)

Domain-driven design (DDD) is a software design approach focusing on modelling software to match a…

4 years ago

Microservices – Onion architecture

A microservice architecture – a variant of the service-oriented architecture (SOA) structural style – arranges…

4 years ago

Unit Of Work pattern concepts in .NET

The Repository pattern and Unit of Work pattern are used together in most time. Unit…

5 years ago

Behavioral Patterns – Visitor

Visitor – It allows the program changes the algorithm and the operations that base on the visitor…

5 years ago