In this article, I'm going to show how to implement the basic CRUD operations in ASP.NET Core Web API in .NET Core 8 and also save the data in the dockerized MS SQL Server.
This is a continuation of my previous article about configuring the MS SQL Server in the Docker Desktop. Kindly refer to the article to know more about the setup.
Requirements
- Visual Studio 2019 or 2022
- .NET 8
- Docker Desktop & SQL Server Container
- Basic understanding of C#, DOTNET Core Framework & Web API's
We need to create a Web API application in Visual Studio 2022 & Choose "Create a new Project".
![Visual Studio]()
Then on the Project Template page, choose the "ASP.NET Core Web API"
![Create new project]()
On the Configure page, Kindly enter the Project Name, and Solution Name and Choose "Next"
![Configure new project]()
Chose the Additional information as it is and then press "Create"
![Additional information]()
Also, create the Folders & Files in the screenshot of Solution Explorer except for the migration folder which was created at the time of migration.
![Solution Explorer]()
Also, install the necessary packages via the Package Manager console.
![Installed packages]()
Kindly copy and paste the code for the respective files.
Student.cs
DataContext.cs
StudentController.cs
Add the MSSQL Server Configuration line on the Program.cs.
Also, kindly add the Database connection to the appsettings.json file. Use your User ID & Password.
Then you need to open the Package Manager console. Type the command to add the migration
After the successful migration, then update-database to update the scheme on the database
Now, you can able to see the Database created on the Docker SQL Server. Open the MS SQL Server Management Studio -> Login using the Docker Credentials -> Refresh -> Able to see the database on the Object Explorer
![Object Explorer]()
Here, I have used the MSSQL server running on the container. Make sure Docker Deamon is running. otherwise, it'll throw an error.
Now, build & run the application. You can able to see the swagger is opened on the browser with the mentioned endpoints.
![Swagger]()
Finally, you can make the basic curd operations from the Swagger and then data will be stored in the database on the Docker Container.
![LocalHost]()
You can see the data from the table and the API call returns the same. Kindly try it from your end and let me know if you are facing any issues.
![Student DB]()
Also, In my next article, I'll show how to deploy this API application in Docker Container.