give me step by step approach for creating the Web API in c# and using the postgresql without using the entity framework for the crud operations
Loading
give me step by step approach for creating the Web API in c# and using the postgresql without using the entity framework for the crud operations
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 29, 2024, 2:34 AM EST
To create a Web API in C# and use PostgreSQL for CRUD operations without Entity Framework, you can follow the following step-by-step approach:
Create a new ASP.NET Core Web API project: Open Visual Studio and create a new ASP.NET Core Web API project. Choose the appropriate target framework and project name.
Install the necessary NuGet packages: Open the NuGet Package Manager Console and install the Npgsql package, which is the PostgreSQL ADO.NET provider for .NET Core. Run the following command:
appsettings.jsonfile, add the connection string for your PostgreSQL database. Replace the placeholders with your actual database details. Here's an example:"ConnectionStrings": { "DefaultConnection": "Host=localhost;Port=5432;Database=YourDatabase;Username=YourUsername;Password=YourPassword" }Create a model class: Create a class that represents your database table. Add the necessary properties and annotations to map the class to the corresponding table in the database.
Create a data access layer: Create a separate class or folder to handle the data access logic. In this class, write methods to perform CRUD operations using raw SQL queries. Use the
NpgsqlConnectionclass to establish a connection to the database and execute queries.Create a controller: Create a controller class to handle the HTTP requests and responses. In this class, inject the data access layer class and implement the necessary CRUD actions (GET, POST, PUT, DELETE) using the methods from the data access layer.
Test the API: Run the application and test the API using a tool like Postman. Send HTTP requests to the appropriate endpoints and verify that the CRUD operations are working as expected.
Vishal YelveRank #178 · 13.6K reputationPosted Jan 29, 2024, 2:49 AM EST
Hi KP,
do refer below articles
https://www.aspsnippets.com/Articles/3841/ASPNet-Core-MVC-Using-Web-API-without-Entity-Framework/
https://www.appsloveworld.com/create-asp-net-core-web-api-without-entity-framework
https://itnext.io/asp-net-core-3-1-entity-framework-core-with-postgresql-with-code-first-approach-33b102e1734f