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. You can also use SQL server 2008 or above.
What Is a Class Library?
A class library contains types, classes, interfaces, and methods that are easily reusable, shareable, distributed, or consumed by an application. Also, a class library project is used for code reusability.
- 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 and stored procedures.
You may also like:
All Things ASP.NET Core and MVC: Tutorials and Articles.
Creating Database and Stored Procedures
Step 1 – Create a Database
Open your SQL server and use the following script to create the “CoreMvcDB” Database
Create database CoreMvcDB
Step 2 – Create a Table
Open your SQL Server and use the following script to create the “tbl_Employee” table.
Step 3 – Create a Stored Procedure
Now, we will create a stored procedure to add Employee data to the database. Open your SQL Server and use the following script to create the procedure.
To insert an Employee Record
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.
Step 2 – Creating a .NET Core Class Library Project
- In the Solution Explorer, right-click on the Chittaranjan solution file, and from the context menu, select the Add -> New Project menu option.
- In the Add New Project dialog, expand the Visual C# node; then, select the .NET Core node in the left side and select the Class Library (.NET Core) project template.
Now, you can give your project a name and select OK to create the .NET Core class library project. Here, my project name is “ChittaDB.”
Now, our Class Library project is created. The solution, “Chittaranjan,” contains one project, named “ChittaDB.”
Step 3 – Add Class Library Project Functionality
In the “ChittaDB” Class library project, I have added two class files for to insert data and save data in the database. To do this:
- Add a class in the Class library project.
- Right-click on the “ChittaDB” project and select the Add a New Item menu option. On the menu option dialog, select VisualC#Items on the left side and select Class on the right-hand side. Then, you can add your class name. Here, the name of our class is EmployeeEntities.cs. Now, select the 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.
- Add another Class in Class Library Project for Insert Methods.
- Right-click on the “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.
For Ado.Net, I am adding “Microsoft.EntityFrameworkCore.SqlServer” in my class library project. This is available in Manage NuGet packages.
Step 5 – Build
Build your project by hitting F5 or by selecting the Build menu item in Visual Studio.
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.
After selecting the project, a New Project dialog will open. Select .NET Core inside the Visual C# menu from the left side panel.
Then, select ASP.NET Core web application from available project templates. Give a name to the project “ChittaWeb” and press OK.
After clicking on the OK button, a new dialog will open to select the project template. You can see two drop-down menus at the top left of the template window. Then, select .NET Core and ASP.NET Core 2.1 from these dropdowns. Select Web application (Model-View-Controller) template and press OK to create the ASP.NET Core MVC project. Then, select the tick mark for configuring for HTTPS.
In Solution, “Chittaranjan” contains two projects; the first project is “ChittaDB.” This is the class library project. The second project name is “ChittaWeb.” This is the ASP.NET Core 2.1 MVC application. Now, our ASP.NET Core MVC application is created.
Now, we can use the .NET Core 2.1 class library functionality in ASP.NET Core 2.1 MVC application.
Step 1 – Add a Class Library Project Reference
To use a class library project in your application, first you must add a reference to the class library project to access its functionality. Right-click on the “ChittaWeb” ASP.NET Core MVC application Dependencies in Solution Explorer and select the Add -> Reference option.
On the next screen, you will see that the ChittaDB is already available. Select ChittaDB and then click the OK button.
Step 2 – Import Namespace and Call Functions
First, add a controller in Asp.Net core MVC application then using Import namespace and call functions.
Right-click on the Controllers folder and select Add New Item.
An Add New Item dialog box will open. Select ASP.NET Core from the left panel; then, select Controller Class from the templates panel, and put make the name EmployeeController.cs. Then, press Add.
In Employeecontroller, I will Import a ChittaDB namespace.
Before you can use a class library project and its classes, you must import the namespace by using the following code.
If your class library project reference is added correctly, as soon as you start typing, “using Chitt..”, you will see that Visual Studio Intellisense will load the namespace in the list.
Once the reference is added, all classes, methods, and public members of the class library project should be available in your ASP.NET Core MVC application. Now, we can call all the methods in the Controller.
Here, I will call all the classes and functions from the Class library project. We will put our business logic into this controller.
To handle database operations, we will create an object of EmployeeDBAccessLayer class inside the EmployeeController class.
To handle the business logic of the
create operation, open EmployeeController.cs and add the following code into it:
Adding View to the Application
To add views for our controller class, we need to create a folder inside Views folder with the same name as our controller and then add our views to that folder.
Right-click on the Views folder, and then Add >> New Folder and name the folder Employee.
Now, right-click on the Views/Employee folder and then select Add >> New Item.
An Add New Item dialog box will open. Select ASP.NET Core from the left panel, select Razor View from templates panel, and make the name Create.cshtml. Then, press OK.
This view will be used to Add new employee data to the database.
Open Create.cshtml and put the following code into it.
Add a New Item
Edit the Views/Shared/_Layout page and add a new menu, Add Employee. For that, add the following code:
Build and Run
Now press F5 to launch the application or run the application. Click on Add Employee to navigate to the Create view.
We have also added validation using
DataAnnotations on the Create View page. If we miss the data in any field while creating an employee record, we will get a required field validation error message.
When the data is saved, the success message will show. From there, click ok. After the data is saved, you can check your database.
In this article, you have learned how to create a .NET Core 2.1 Class library project using ADO.NET with the help of Visual Studio 2017. You have also learned how to consume a .NET Core class library in an ASP.NET Core MVC application for insert data.