Posts

Showing posts from March 31, 2025

Tips & Tricks for Working with Dapper in .NET Core

Tips & Tricks for Working with Dapper in .NET Core Dapper is a lightweight ORM that is simple and efficient, but like any tool, there are advanced features and best practices to ensure you use it effectively. This article explores some of Dapper's advanced features and common pitfalls developers should be aware of.   Advanced Features in Dapper MultiMapping Dapper allows you to map multiple result sets to multiple objects with its MultiMapping feature. This is particularly useful when working with joins or complex queries where you need to map different tables to different classes. Example: var sql = @"     SELECT         e.Id, e.Name, e.Email,         d.Id AS DepartmentId, d.Name AS DepartmentName     FROM Employees e     INNER JOIN Departments d ON e.DepartmentId = d.Id";   using (var connection = new SqlC...

Real-World Example: Building a Simple Blog with Dapper in .NET Core

Real-World Example: Building a Simple Blog with Dapper in .NET Core Dapper is a lightweight and high-performance micro-ORM that provides direct SQL execution while still offering powerful object mapping capabilities. In this article, we'll build a simple blog application that demonstrates CRUD operations on articles using Dapper for data access.   Project Setup 1. Create a .NET Core Web API Project Open a terminal and run: dotnet new webapi -n DapperBlogApp cd DapperBlogApp 2. Install Required Packages To use Dapper, install the necessary NuGet packages: dotnet add package Dapper dotnet add package System.Data.SqlClient 3. Set Up the Database Create a simple Articles table in SQL Server: CREATE TABLE Articles (     Id INT PRIMARY KEY IDENTITY,     Title NVARCHAR(200) NOT NULL,     Content NVARCHAR(MAX) NOT NULL,     Author NVARCHAR(100) NOT NULL,    ...