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.
Conditions
Docker Desktop & SQL Server Container; Visual Studio 2019 or 2022;.NET 8
basic familiarity with the DOTNET Core Framework, Web APIs, and C#
We must select “Create a new Project” in Visual Studio 2022 in order to construct a Web API application.
Then on the Project Template page, choose the “ASP.NET Core Web API”
Next, select the “ASP.NET Core Web API” option from the Project Template page.
Press “Create” after selecting the Additional information exactly as it is.
Also, create the Folders & Files in the screenshot of Solution Explorer except for the migration folder which was created at the time of migration.
Also, install the necessary packages via the Package Manager console.
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
update-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
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.
Finally, you can make the basic curd operations from the Swagger and then data will be stored in the database on the Docker Container.
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.
Also, In my next article, I’ll show how to deploy this API application in Docker Container.
Best and Most Recommended ASP.NET Core 8 Hosting
Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 8 web ranking. HostForLIFEASP.NET is highly recommended. In Europe, HostForLIFEASP.NET is the most popular option for first-time web hosts searching for an affordable plan.
Their standard price begins at only € 3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFEASP.NET guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.
By providing reseller hosting accounts, HostForLIFEASP.NET also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.