Introduction
Hello, in this tutorial, I will explain how to create .Net Core web API 7.0 using Visual Studio 2022. Also, I will explain you to do CRUD operations using Postman. So Let's start.
Prerequisite: To work with .Net core API 7.0, you must have a visual studio on your machine, in my case, In this tutorial I'm using Visual Studio 2022(Community). Also to work with the database, I'm using SQL Server Management Studio 2019. So, I'm considering you already have both the tools in your machine. If you don't have, please download and install it on your machine. Complete Video Tutorial is available here.
Step 1. Now, let's start by creating a database, and in that database, create a table called Student.
Open SQL Server Management Studio 2019 and execute the below SQL queries.
Create Database IP_DBFirst
Use IP_DBFirst
Now execute below SQL query to create a table, this table contains five columns.
Create table Student(Id int primary key identity(1,1), FirstName varchar(20), LastName varchar(20), Email varchar(40), Mobile varchar(10))
Also, Add some sample data to the above-created table by executing the below queries, these queries will insert two records in your student table.
Insert into Student(FirstName,LastName,Email,Mobile)Values('John','A', '[email protected]','8823456789')
Insert into Student(FirstName,LastName,Email,Mobile)Values('Lucy','Kerri', '[email protected]','9898765456')
Now check the records in your student table. To check records, execute the below query to verify that the data is inserted successfully.
Select * from Student
Step 2. In this step, Let's create a web API project, Open Visual Studio 2022, and select the option "Create a new project", As shown below in the given screenshot.
Once you click on the "Create a new project" a new popup window will open; in the ASP.NET Core Web API section, select the "ASP.NET Core Web API" template from the right-side window and click on the next button.
After clicking on the next button, a new popup will open, here you need to provide the Project name, Solution name will be created itself, so there is no need to change the solution name. Select the location of the project to save it on a local machine, then click on the next button, as below screenshot.
After clicking on the next button, you will get a new popup, select the framework version as ".Net 7.0 (Standard Term Support)", and then click on the Create button. Follow the screenshot below.
Now you are done with your Web API project creation, and you will see the project files as the given below screenshot.
Now it's time to install some nuget packages. To add packages, Right click on the project, then select "Manage Nuget Packages…" from the list. You will see something like the below screenshot. Goto browse section.
Now browse the listed below packages and install them. Make sure you are installing the latest version of the packages.
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.SQLServer
- Microsoft.EntityFrameworkCore.Tools
In the next step, you need to add a connection string in the appsettings.json file.
"ConnectionStrings": {
"StudentConnectionString": "Data Source=GAUTAM\\SQLEXPRESS; Database:IP_DBFirst; User Id:sa; Password:123456;"
}
Make sure you are adding the connection string at the right place and providing the correct details. To avoid typing mistakes, copy the details and paste Also don't forget to change connection string details, as in the given example it's my connection string details.
Now your appsettings.json file will look like as below.
Now it's time to create a DBContext class, let's create a folder in the project named "Models" Inside this folder, we will create our DBContext class. Goto Tools, then NuGet Package Manager, and then Package Manager Console.
In the Package Manager Console window, write the below command. Make sure you need to provide your connection string details. In my case, it's my details, and it will be different in your case.
Scaffold-DbContext "Server=GAUTAM\SQLEXPRESS; Database=IP_DBFirst; User Id=sa; Password=123456; TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
After executing the above command, if there is no error, you will see two files will be generated by the command. The first file will be DbContext, and another will be Student class as your database table name.
Now it's time to create a controller, Right-click on the controller folder, Select Add, and then Controller. The new popup will be opened for you, as below screenshot. From the left panel, select API, then from the right panel, Select template "API Controller with actions, using Entity Framework".
Then Click on the Add button, and again new popup will get opened, from the Model class, select the Student class, and from DbContext class, select IpDbfirstContext class. In your case, it might be different based on your database and class name. Leave the controller name as it is, then click the Add button. It will take a few seconds to generate the code.
Now check the StudentsController, all (GET, POST, PUT, and DELETE) action methods will be generated by scaffolding. Your controller code will look like below.
We are done with the controller creation, Now, let's configure the database context and register it with the dependency injection container by reading the required information from the appsettings.json file.
In the previous, we have Startup.cs file in our project but with Visual Studio 2022, there is no Startup.cs file. So, all the dependency injection code we have to write in Program.cs file.
Add the below code to Program.cs file. And your file will look like the below screenshot.
IConfiguration configuration;
configuration=new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
builder.Services.AddDbContext<IpDbfirstContext>
(option => option.UseSqlServer(configuration.GetConnectionString("StudentConnectionString")));
That's all you need to do to work with .Net core API 7.0
Let's test whether the API is working fine or not. Just run your project, and you will see swagger UI as below screenshot.
Expand the first Get API and click on the Try it out button.
Now click on the Execute button to see the result.
After clicking on the Execute button, it will get the data from the database and display it as the screenshot below.
Hope this step-by-step tutorial helps you a lot. And you will be able to work on the ASP.NET Core API with Database first approach. Also, please provide your feedback. It helps me to improve the content quality.