Think Big, Act Small, Fail Fast and Learn Rapidly


Onion Architecture In ASP.NET Core MVC

Bài viết khá hay về ASP.NET Core, có thể tham khảo về kiến trúc


A Visual Studio 2015 project which shows how to perform the create, read, update and delete operations in the ASP.NET Core MVC application using onion architecture with Entity Framework Core Code First approach.

The code illustrates the following topics:

  1. Listings, create, update and delete operations.
  2. One to one relationship in Entity Framework Core.
  3. Create, Update and Delete operations perform in bootstrap modal pop up with tag helpers.
  4. Database design using entity framework core code first approach with fluent API.
  5. Entity classes and repository in separate class library project and use migration to create database from it.
  6. Dependency injection for DbContext, Repository and service.

Article: Onion Architecture In ASP.NET Core MVC

Getting Started

To build and run this sample as-is, you must have Visual Studio 2015 installed. In most cases you can run the application by following these steps:

  1. Download and extract the .zip file.
  2. Open the solution file in Visual Studio.
  3. Change connection string in the appsettings.json file of the web project.
  4. Choose OA.Repo project in package manager console and run the following command for migration and create database.
    • Tools –> NuGet Package Manager –> Package Manager Console
    • PM> Add-Migration MyFirstMigration
    • PM> Update-Database
  5. Run the application.

Running the Sample

To run the sample, hit F5 or choose the Debug | Start Debugging menu command. You will see the user list screen. From this screen you have user listing screen as shown in below figure.

Figure 1: User listing

Now click on “Add User” button to add new user in the application as per following screen.

Figure 2: Add User

As per figure 1, Edit button uses to edit individual user as per following figure.

Figure 3: Edit User

As per figure 1, Delete button uses to delete individual user as per following figure.

Figure 4: Delete User

Source Code Overview

Most of folders play same role as in MVC application but there are following more folder and files.

  1. wwwroot: It holds static js and css files.
  2. OA.Data: It’s separate class library project. It holds Entities and table configuration classes which create database.
  3. OA.Repo: It’s a class libraray project which holds DataContext and Repository.
  4. OA.Service: It’s a class libraray project which holds service interface which communicates to UI.
  5. appsettings.json:It holds database connection string.
  6. Migrations: It holds database migration files.