Introduction
In this article, I am going to show you how to create a .NET Core 2.1 Class Library Project using ADO.NET.
This class library project contains business logic functionality for inserting data into a database. Then, I am consuming this class library project in ASP.NET Core 2.1 MVC Application.
We will be using Visual Studio 2017 (version 15.9.13 or above) and SQL Server 2017 or you can use SQL server 2008 or above version.
What is the Class Library?
A class library contains types, classes, Interfaces and methods that are easily reusable, shareable, distributed or consume by an application. Also, a class library project is used for code reusability, Code reusability is one of the key features of modern programming languages. It helps to reduce Errors, code duplication and reduce the development time.
Prerequisites
- Install Visual studio 2017 Updated version 15.9.13
- Install .Net Core SDK 2.1 or above
- SQL Server 2017
Now we will create our .Net Core 2.1 Class library project.
First of all, we will create a Database, table and a stored procedure.
Creating Database, Table, and Stored Procedures
Step 1 - To create a Database
Open your SQL server and use the following script to create the “CoreMvcDB” Database
Create database CoreMvcDB
Step 2 - To Create a Table
Open your SQL Server and use the following script to create “tbl_Employee” table.
- create table tbl_Employee
- (
- Sr_no int not null primary key identity(1,1),
- Emp_Name nvarchar(250),
- City nvarchar(100),
- State nvarchar(100),
- Country nvarchar(100),
- Department nvarchar(50)
- )
Step 3 - To create a stored procedure
Now we will create a stored procedure to Add Employee data to DataBase.
Open your SQL Server and use the following script to create the procedure.
To insert an Employee Record
- create procedure sp_Employee_Add
- @Emp_Name nvarchar(250),
- @City nvarchar(100),
- @State nvarchar(100),
- @Country nvarchar(100),
- @Department nvarchar(50)
- AS
- BEGIN
- Insert into tbl_Employee(Emp_Name,City,State,Country,Department)
- values(@Emp_Name,@City,@State,@Country,@Department)
- END
Now, our database part has been completed. So now we will create a .Net core 2.1 class library project.
Creating A .NET Core 2.1 Class Library Project
Step 1 - Creating a class library Empty solution
First creating an Empty solution for our class library project, it contains our class library project and its related projects. A Visual Studio Solution just serves as a container for one or more projects.
- Open Visual Studio menu bar, select File -> New -> Project.

- After selecting the project, A "New Project" dialog will open, expand the “Other ProjectTypes” node and select “Visual Studio Solutions”. Name the Solution “Chittaranjan” and select the OK button.
Note
My solution Name is “Chittaranjan”.

The full structure of Empty solution will look like this.

Step 2 - Creating our .Net Core class library project
- In the Solution Explorer, right-click on the Chittaranjan solution file and from the context menu, you can select Add -> New Project menu option.

- In the Add New Project dialog, expand the Visual C# node, then you can select .NET Core node in the left side and select Class Library(.NET Core) project template.
Now you can give your project name and select OK to create the .NET Core class library project.
Here my project name is “ChittaDB”.
Now the solution structure is given below,

Now our Class library project is created. Solution ‘Chittaranjan’ contains one project and the project name is “ChittaDB”.
Step 3 - Add Class Library Project Functionality
In “ChittaDB” Class library project I have Added 2 Class files for Insert data and save data in the database.
- Add a class in Class library project
- Right-click on “ChittaDB” project and select add a new Item menu option. On the menu option dialog, Select VisualC#Items in the left side and select Class in the right-hand side. Then you can put your class name. Here Name of our class is EmployeeEntities.cs, Select Add button. This class will contain our Employee properties.


- Now, open the EmployeeEntities.cs class and put the following code in it. We are also adding the required validators to the fields of EmployeeEntities class, so we need to use System.ComponentModel.DataAnnotations at the top.
- using System.ComponentModel.DataAnnotations;
- namespace ChittaDB
- {
- public class EmployeeEntities
- {
- [Required]
- public string Emp_Name { get; set; }
- [Required]
- public string City { get; set; }
- [Required]
- public string State { get; set; }
- [Required]
- public string Country { get; set; }
- [Required]
- public string Department { get; set; }
- }
- }
- Add another Class in Class Library Project for Insert Methods.
Right-click on “ChittaDB” project and select add a new Item menu option. On the menu option dialog, Select VisualC#Items in the left side and select Class in the right-hand side. Then you can put your class name. Here Name of our class is EmployeeDBAccessLayer, Select Add button. This class will contain our database related operations.

Now, The Class Library Project structure is given below.
Open EmployeeDBAccessLayer.cs and put the following code to handle the database operations. Make sure to put your connection string.
Note
For Ado.Net, I am adding “Microsoft.EntityFrameworkCore.SqlServer” in my class library project. This is available in Manage NuGet packages.
For Ado.Net, I am adding “Microsoft.EntityFrameworkCore.SqlServer” in my class library project. This is available in Manage NuGet packages.
- using System.Data;
- using System.Data.SqlClient;
- namespace ChittaDB
- {
- public class EmployeeDBAccessLayer
- {
- SqlConnection con = new SqlConnection("put your connection string here");
- public string AddEmployeeRecord(EmployeeEntities employeeEntities)
- {
- try
- {
- SqlCommand cmd = new SqlCommand("sp_Employee_Add", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("@Emp_Name", employeeEntities.Emp_Name);
- cmd.Parameters.AddWithValue("@City", employeeEntities.City);
- cmd.Parameters.AddWithValue("@State", employeeEntities.State);
- cmd.Parameters.AddWithValue("@Country", employeeEntities.Country);
- cmd.Parameters.AddWithValue("@Department", employeeEntities.Department);
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- return ("Data save Successfully");
- }
- catch (Exception ex)
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- return (ex.Message.ToString());
- }
- }
- }
- }
Step 5 - Build
Build your project by hitting F5 or by selecting the Build menu item in Visual Studio. A successful build looks like below:
Step 6 - Consuming A Class Library Project
Here I am consuming a class library project in ASP.NET Core 2.1 MVC application.
First, we will create an ASP.NET Core 2.1 MVC application.
Open Visual Studio and select File -> New -> Project.
















Loran HaydenPosted Feb 22, 2021, 12:10 PM
Use dependency injection - create a class constructor that takes an object of type ApplicationDbContext and set it to a readonly private ApplicationDbContext _db like public MyClass(ApplicationDbContext db){ ... _db = db;...}
Muteen ul HaqPosted Jan 20, 2021, 4:53 AM
Nice article. I want to know how to access connection string define in appsettings.json in DB Access Layer class library?
Chinmaya DashPosted Sep 9, 2019, 5:26 AM
Nice article
Rajanikant HawaldarPosted Sep 6, 2019, 9:36 AM
nice article chittaranjan
prasanna pPosted Sep 5, 2019, 1:58 AM
Nice article,Thanks for sharing Chittaranjan
Amit MohantyPosted Sep 4, 2019, 11:59 PM
Nicely described.
Abhishek MishraPosted Sep 4, 2019, 11:55 AM
This is written very well. Cool !!!