Asp.NET Core Web API is a popular choice for building backend services, and while Entity Framework is a common database management tool, it's not the only option. In this guide, we'll explore creating an Asp.NET Core Web API with a database without using Entity Framework, focusing on Dapper, a simple, fast, and practical micro-ORM.

Dapper is a lightweight, open-source ORM that helps interact with databases using plain SQL. It's an excellent alternative when you want to have more control over your database interactions and avoid the overhead that sometimes comes with Entity Framework.

Setting Up the Project
To get started, create a new Asp.NET Core Web API project with the following command:

dotnet new webapi -n NoEfWebApi
Adding the Database Context
First, let's add a SQLite database for simplicity. In the appsettings.json file, add the following connection string:

"ConnectionStrings": {
"DefaultConnection": "Data Source=blogging.db"
}
Next, create a new class DatabaseContext.cs to handle database operations.
Setting Up Dapper
Install the Dapper package via NuGet to start using it in the project:

dotnet add package Dapper
Now, let's configure Dapper with the DefaultConnection from appsettings.json.
Finally, create a new model class, say Blog.cs, to represent the data in the database.
Creating the API Controller

Create a new API controller, BlogController.cs, to handle CRUD operations using Dapper.
Getting All Blogs









Let's start by creating an action to retrieve all blogs from the database.
Creating a New Blog
Next, create an action to insert a new blog into the database.
Updating and Deleting Blogs
Following the same pattern, create actions to update and delete blogs.
Here's an example of the CreateAsync method:
```csharp
[HttpPost]
public async TaskMigrating the Database
To create the Blogs table in the SQLite database, run the following script:
```sql CREATE TABLE Blogs ( Id INTEGER PRIMARY KEY AUTOINCREMENT, Url NVARCHAR(200) NOT NULL, Name NVARCHAR(50) NOT NULL, Description NVARCHAR(255) ); ```
Seeding the Database
To add initial data to the database, create a method in the DatabaseContext class to seed the data:
```csharp
public async Task InitAsync()
{
using var connection = new SqlConnection(_config.GetConnectionString("DefaultConnection"));
var blogs = new List Finally, call this method in the Configure method of the Startup.cs file:
```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DatabaseContext context) { context.InitAsync().Wait(); // ... } ```