<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n" -->

---
title: Getting Started with Dapper in .NET | daily.dev
description: Dapper is a lightweight micro ORM for .NET that focuses on fast query-to-object mapping without the overhead of a full ORM like Entity Framework Core. The...
canonical: https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Getting Started with Dapper in .NET | daily.dev
og:description: Dapper is a lightweight micro ORM for .NET that focuses on fast query-to-object mapping without the overhead of a full ORM like Entity Framework Core. The...
og:url: https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n
og:image: https://api.daily.dev/og/posts/45v0jUx9N.png
og:image:alt: Getting Started with Dapper in .NET
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Getting Started with Dapper in .NET

**[NikolaTech](https://daily.dev/sources/nikolatech)** · 8 min read · 0 upvotes · 0 comments

## Summary

Dapper is a lightweight micro ORM for .NET that focuses on fast query-to-object mapping without the overhead of a full ORM like Entity Framework Core. The walkthrough covers installing Dapper and Npgsql, setting up a connection factory with dependency injection, using ExecuteAsync for inserts and updates, the various Query methods (Query, QuerySingle, QueryFirst and their variants), handling transactions via BeginTransaction, Dapper.Transaction, and TransactionScope, and calling PostgreSQL stored procedures by setting CommandType to StoredProcedure. Parameterized queries are emphasized as a defense against SQL injection.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nikolatech.net/blogs/dapper-asp-dotnet-core>

## Questions this post answers

### How do I call a PostgreSQL stored procedure using Dapper?

Pass the stored procedure's name as the SQL parameter to ExecuteAsync and set the commandType parameter to CommandType.StoredProcedure. Dapper then treats the string as a procedure name rather than a raw SQL statement, executing it directly against the database. This works well for encapsulating business logic, such as a procedure that cancels old orders based on their creation time and status.

_daily.dev surfaces practical data-access patterns like stored procedure calls for developers building with Dapper._

### What is the difference between QuerySingleOrDefault and QueryFirstOrDefault in Dapper?

QuerySingleOrDefault returns a single row or null if none is found but throws an exception if more than one row matches, while QueryFirstOrDefault returns the first row or null without throwing even if multiple rows exist. Choosing between them depends on whether you want to enforce uniqueness of the result or simply need the first matching record regardless of duplicates.

_Developers picking the right Dapper query method can find comparisons like this through daily.dev._

### How do I set up a reusable database connection in Dapper with dependency injection?

Define an ISqlConnectionFactory interface with an OpenConnection method, then implement it per provider, for example a PostgresConnectionFactory wrapping NpgsqlConnection that opens the connection before returning it. Register the factory as a singleton in the DI container, passing the connection string from configuration, so handlers can resolve it and call OpenConnection to get an IDbConnection for Dapper calls.

_daily.dev helps .NET developers track setup patterns like connection factories when adopting Dapper._

## Similar posts on daily.dev

- [How to Work With Dapper in .Net](https://daily.dev/posts/how-to-work-with-dapper-in-net-yzgrlyllb) · freeCodeCamp · 17 upvotes · 0 comments
- [Dapper in Depth with ASP.NET Core 10](https://daily.dev/posts/dapper-in-depth-with-asp-net-core-10-oad4bk2bw) · C\# Corner · 1 upvotes · 0 comments
- [Building CRUD API with Dapper and Dapper Plus in ASP.NET Core](https://daily.dev/posts/building-crud-api-with-dapper-and-dapper-plus-in-asp-net-core-pzojrv4vq) · We Are .NET · 0 upvotes · 0 comments

---

Tags: [#.net](https://daily.dev/tags/.net), [#postgresql](https://daily.dev/tags/postgresql), [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Getting Started with Dapper in .NET","url":"https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n"},"datePublished":"2026-10-04T13:05:45.370Z","dateModified":"2026-10-04T13:19:21.173Z","description":"Dapper is a lightweight micro ORM for .NET that focuses on fast query-to-object mapping without the overhead of a full ORM like Entity Framework Core. The...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/23a75df1e9087466573f67f8860d1d98?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/23a75df1e9087466573f67f8860d1d98?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"NikolaTech","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"NikolaTech","logo":"https://media.daily.dev/image/upload/s--1h2fO6FL--/f_auto,q_auto/v1791119050/logos/nikolatech?_a=BAMAMicg0","url":"https://daily.dev/sources/nikolatech"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":".net,postgresql,c#","timeRequired":"PT8M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"NikolaTech","item":"https://daily.dev/sources/nikolatech"},{"@type":"ListItem","position":3,"name":"Getting Started with Dapper in .NET"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/getting-started-with-dapper-in-net-45v0jux9n#faq","mainEntity":[{"@type":"Question","name":"How do I call a PostgreSQL stored procedure using Dapper?","acceptedAnswer":{"@type":"Answer","text":"Pass the stored procedure's name as the SQL parameter to ExecuteAsync and set the commandType parameter to CommandType.StoredProcedure. Dapper then treats the string as a procedure name rather than a raw SQL statement, executing it directly against the database. This works well for encapsulating business logic, such as a procedure that cancels old orders based on their creation time and status. daily.dev surfaces practical data-access patterns like stored procedure calls for developers building with Dapper."}},{"@type":"Question","name":"What is the difference between QuerySingleOrDefault and QueryFirstOrDefault in Dapper?","acceptedAnswer":{"@type":"Answer","text":"QuerySingleOrDefault returns a single row or null if none is found but throws an exception if more than one row matches, while QueryFirstOrDefault returns the first row or null without throwing even if multiple rows exist. Choosing between them depends on whether you want to enforce uniqueness of the result or simply need the first matching record regardless of duplicates. Developers picking the right Dapper query method can find comparisons like this through daily.dev."}},{"@type":"Question","name":"How do I set up a reusable database connection in Dapper with dependency injection?","acceptedAnswer":{"@type":"Answer","text":"Define an ISqlConnectionFactory interface with an OpenConnection method, then implement it per provider, for example a PostgresConnectionFactory wrapping NpgsqlConnection that opens the connection before returning it. Register the factory as a singleton in the DI container, passing the connection string from configuration, so handlers can resolve it and call OpenConnection to get an IDbConnection for Dapper calls. daily.dev helps .NET developers track setup patterns like connection factories when adopting Dapper."}}]}
```

