Categories: ASP.NETASP.NET Core

Building MVC pattern project by Visual Studio 2017 ( ASP.Net Core Part )

Step 9. Create a new class library project called the infra project of .NET Core framework, which name is “Enterprise_Dot_Net_Core_WebApp.Infra.” After Visual Studio build-over the class library project, create two folders under this project, such as DBContext folders and the Repositories folder.

Fig 12 The infra project solution explorer

Note: Database context folder/file naming rules don’t set the “DbContext” because the entity framework has a tool name that is DbContext. If you set your database context file name is DbContext, the Visual Studio compiler will miss the entity framework’s Dbcontext reference. “Naming rules” are important! Important! Important!!

Step 10. Create the class file, which name is “DemoDbContext,” under the DBContext folder.

Fig 13 Infra project solution explorer about the DBContext folder

Note: This sample project is to add Microsoft.EntityFramework.Sqlserver to the reference part of the infra project. If you want to use other databases such as Mysql, SQLite, and PostgreSQL, you must install other Microsoft.EntityFramework database.

Step 11. Edit the connection database source code in the DemoDbContext file. DemoDbContext file will get the database connection string from the appsettings file of the web project. Don’t worry about it.

Fig 14 DBContext source code
 using Enterprise_Dot_Net_Core_WebApp.Core.Entities;
 using Microsoft.EntityFrameworkCore;
 using System;
 using System.Collections.Generic;
 using System.Text;
 namespace Enterprise_Dot_Net_Core_WebApp.Infra.DBContext
 {
     public class DemoDbContext : DbContext
     {
         public DemoDbContext(DbContextOptions options) : base(options)
         { }
         public DbSet<Enterprise_MVC_Core> Enterprise_MVC_Cores { get; set; }      
     }
}

Note: In this file, I only set the table by the “DbSet” method. You can select more tables or other forms in this file.

Step 12. Create the class file, which name is “Enterprise_MVC_Repository,” under the Repositories folder.

Fig 15 Infra project solution explorer

Step 13. Edit some codes in the Enterprise_MVC_Repository file. This code will accord the interface file of the core project to create the methods in this file. The infra project is based on Microsoft.EntityFrameworkCore library, so you write code that must follow the Microsoft.EntityFrameworkCore rules.

Fig 16 Infra project repository 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. (Link)

Page: 1 2 3 4

davidsky69

View Comments

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…

5 years ago